New to Zend and having a problem with routing.
I can get routing working using the application.ini file but cannot seem to get routing working using the bootstrap. I have looked at quite a few tutorials but most seem to apply to older versions of Zend Framework. I am using 1.10.
In my Bootstrap I have:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initRouter()
{
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
'aaa',
array(
'controller' => 'index',
'action' => 'browse'
)
);
$router->addRoute('test', $route);
}
}
开发者_如何学运维
And my index.php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
If I point my browser to http://www.mysite.com/aaa
I get the following error
An error occurred
Page not found
Exception information:
Message: Invalid controller specified (aaa)
Stack trace:
#0 /usr/share/php/libzend-framework-php/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#1 /usr/share/php/libzend-framework-php/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch()
#2 /usr/share/php/libzend-framework-php/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#3 /var/www/mysite.com/directory001/public/index.php(26): Zend_Application->run()
#4 {main}
Request Parameters:
array (
'controller' => 'aaa',
'action' => 'index',
'module' => 'default',
)
This seems so straight forward but does not seem to work.
Can anybody see what I might be doing wrong?
Seems like you're hitting a default route, so my best guess is your route is not registering properly.
I haven't ever used bootstrapping, so maybe there's your problem. Try to follow the execution path and see if the whole router is not created from scratch down the path again (i.e. overriding yours).\
If that's not it, check what your request_uri is. Router is matching your route to $request->getPathInfo(). So see what that returns as well.
Don't be afraid to browse through the source. I have tried to make it as readable as possible:
http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Controller/Router/Rewrite.php
Your code looks correct to me, in my bootsrap I have the following:
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('sitemap', new Zend_Controller_Router_Route('sitemap.xml', array('controller'=>'index','action'=>'sitemap')));
Changed to:
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('sitemap', new Zend_Controller_Router_Route('si', array('controller'=>'index','action'=>'sitemap')));
Both still work, either hitting sitemap.xml to si.....
What is in your application.ini file?
Instead of running in your index.php:
$application->bootstrap()->run();
In my application.ini I have, this starts the application including the bootstrap - not sure if it would make any difference, I haven't really looked into the bootstrap once I got it working:
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
The Error is Simple. Change,
$router->addRoute('test', $route);
to
$router->addRoute('aaa', $route);
I was looking for routing with BootStrap.php and got your post. Then i was also stuck with the same Error. Later noticed this and Found this working.
Thanks to you too
$router = Zend_Controller_Front::getInstance()->getRouter();
$catRoute = new Zend_Controller_Router_Route_Static(
'aaa',
array(
'controller' => 'index',
'action' => 'browse'
)
);
$router->addRoute('category', $catRoute);
精彩评论