Zend: Unable to start the router from Bootstrap.php

New to Zend and the problem with routing.

I can get routing using the application.ini file, but I can't get routing to work using bootstrap. I looked through quite a few tutorials, but most of them seem to be related to older versions of the 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', ) 

It seems so straightforward, but doesn't seem to work.

Can anyone understand what I can do wrong?

+4
source share
4 answers

It looks like you are getting into the default route, so as far as I understand, your route is not being registered properly.

I have never used a download, so maybe you have a problem. Try following the execution path and see if the entire router is generated from scratch along the path again (i.e., Override yours). \

If not, check what request_uri is. The router matches your route in $ request-> getPathInfo (). See what it also gives.

Do not be afraid to browse the source. I tried to make it as readable as possible:

http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Controller/Router/Rewrite.php

+1
source

Your code looks correct for me, in my bootstrap, 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 by clicking sitemap.xml on si .....

What is included in your application.ini file?

Instead of running in index.php:

 $application->bootstrap()->run(); 

In my application.ini application, I have an application that includes loading - not sure if this will make any difference, I really did not look at the boot block as soon as I got it working:

 bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap" 
+1
source

The error is simple. Edit

 $router->addRoute('test', $route); 

before

 $router->addRoute('aaa', $route); 

I searched for routing using BootStrap.php and got your post. Then I, too, got stuck with the same error. Later noticed this and found that it works.

Thank you too

+1
source
 $router = Zend_Controller_Front::getInstance()->getRouter(); $catRoute = new Zend_Controller_Router_Route_Static( 'aaa', array( 'controller' => 'index', 'action' => 'browse' ) ); $router->addRoute('category', $catRoute); 
+1
source

Source: https://habr.com/ru/post/1335593/


All Articles