Welcome! Anonymous

Create a new module and problem of routers

Guides on using TomatoCMS modules

Create a new module and problem of routers

Postby cakeowiec » Tue Jul 27, 2010 9:19 am

My routers:

Code: Select all
routes.home_index.type = Zend_Controller_Router_Route_Static
routes.home_index.route = "/"
routes.home_index.defaults.module = "home"
routes.home_index.defaults.controller = "home"
routes.home_index.defaults.action = "index"


OR

Code: Select all
routes.home_index.type = "Zend_Controller_Router_Route_Regex"
routes.home_index.route = "/"
routes.home_index.defaults.module = "home"
routes.home_index.defaults.controller = "home"
routes.home_index.defaults.action = "index"


Index function not working - How to do that this function was called on the home page ?
cakeowiec
 
Posts: 7
Joined: Sun Jul 18, 2010 3:39 pm

Re: Create a new module and problem of routers

Postby phuoc69 » Thu Jul 29, 2010 5:59 am

Hi,
Do you have home module?
And can you post the error message here and what version of TomatoCMS you are using?
Image
Nguyen Huu Phuoc, TomatoCMS's Founder
Skype: phuoc.69
Gtalk: phuoc6982
Phone: (84) 123 213 8486
Twitter: http://twitter.com/phuoc69
phuoc69
 
Posts: 335
Joined: Thu Jan 07, 2010 3:49 am

Re: Create a new module and problem of routers

Postby cakeowiec » Thu Jul 29, 2010 4:36 pm

Yes, I have home module and TomatoCMS 2.0.7

Function in the file 'HomeController.php' isn't called, there is also no error messages


File application\modules\home\controllers\HomeController.php
Code: Select all
class Home_HomeController extends Zend_Controller_Action
{
   
   /**
    * @return void
   */
   public function indexAction() {
      echo 'test';
      //$this->_redirect( '/test' );
   }
}


When I set:
Code: Select all
routes.home_index.route = "/whatever"

and go to this url - it works - but I want to run a module on the home page - so I thought that it is enough to set:
Code: Select all
routes.home_index.route = "/"
cakeowiec
 
Posts: 7
Joined: Sun Jul 18, 2010 3:39 pm

Re: Create a new module and problem of routers

Postby phuoc69 » Thu Jul 29, 2010 4:52 pm

I think the problem is caused by we register other route for homepage.
Please look at the _initRoutes function from /application/Bootstrap.php file:

Code: Select all
protected function _initRoutes()
{
   $this->bootstrap('FrontController');
   $front = $this->getResource('FrontController');
   
   // (1)
   $routes = Tomato_Module_Loader::getInstance()->getRoutes();
   $front->setRouter($routes);
   
   $front->getRouter()->removeDefaultRoutes();

   // (2)
   $front->getRouter()->addRoute(
      'index',
      new Zend_Controller_Router_Route('/', array(
         'module'     => 'default',
         'controller' => 'Index',
         'action'     => 'index',
      ))
   );
}

(1): The front controller add all routes configured in our routes files, including your home_index route.
(2): It is registered a index route which map with "/".

So, you can comment the (2) section and see what happen.

PS: In 2.0.7.1, the home page is mapped with core_index_index route defined by core module, index controller and index action.
This controller action does not show anything.
If you want to customize the output of homepage, you can customize the home.xml (in 2.0.7 and earlier) or core_index_index.xml (in 2.0.7.1 which is coming out soon) in template directory (/application/templates/NameOfTemplate/layouts)
Image
Nguyen Huu Phuoc, TomatoCMS's Founder
Skype: phuoc.69
Gtalk: phuoc6982
Phone: (84) 123 213 8486
Twitter: http://twitter.com/phuoc69
phuoc69
 
Posts: 335
Joined: Thu Jan 07, 2010 3:49 am

Re: Create a new module and problem of routers

Postby cakeowiec » Fri Jul 30, 2010 12:54 am

Thanks for your reply.

Deleted the (2) and still doesn't work so I did:
Code: Select all
protected function _initRoutes() {
   $this->bootstrap('FrontController');
   $front = $this->getResource('FrontController');
       
   $routes    = Tomato_Module_Loader::getInstance()->getRoutes();
   $addIndex   = true;
   $front->setRouter($routes);
      
   /**
   * Don't use default route
   */
   $front->getRouter()->removeDefaultRoutes();
   
   foreach( $front->getRouter()->getRoutes() as $route )
      if( ( $route instanceof Zend_Controller_Router_Route || $route instanceof Zend_Controller_Router_Route_Static ) && $route->match('/',true) !== false ) {
         $addIndex = false; break;
      }
      
   /**
   * Zend Framework 1.10.0 requires route which matchs with "/"
    * @since 2.0.3
   */
   if( $addIndex )
      $front->getRouter()->addRoute(
         'index',
         new Zend_Controller_Router_Route('/', array(
            'module'     => 'default',
            'controller' => 'Index',
            'action'     => 'index',
         ))
      );
      
   /**
   * Add routes for static pages
    */
   $config = TOMATO_APP_DIR . DS . 'config' . DS . 'layout.ini';
   if (file_exists($config)) {
      $config = new Zend_Config_Ini($config, 'layouts');
      $config = $config->toArray();
      $config = $config['layouts'];
      foreach ($config as $key => $value) {
         if ('static' == $value['type'] && ( $addIndex || $value['url'] != '/' ) ) {
            $front->getRouter()->addRoute(
               'index_static_'.$key,
                   new Zend_Controller_Router_Route($value['url'],
                  array(
                     'module'     => 'default',
                     'controller' => 'Index',
                     'action'     => 'index',
                  ))
            );
         }
      }
   }
}


However, this does not solve my main problem - I care about this so as not change the core files TomatoCMS - because when the day comes when it is released a new version - I'll have to remember which files have been changed rather than simply update my application :D

In my opinion TomatoCMS should consist of a Core without the editing and I can to do everything whih using modules, plugins, widgets, hooks and their right configuration - in this case when you add automatic update module (which I hope will be soon ;)) - will not be a situation that after downloading and installing something stops working.

Therefore, my solution to this problem, which posted above - but probably not the best - make that no longer need to interfere in the Core - but all depends on the settings in the modules
cakeowiec
 
Posts: 7
Joined: Sun Jul 18, 2010 3:39 pm

Re: Create a new module and problem of routers

Postby phuoc69 » Fri Jul 30, 2010 2:47 am

Hi,

Clearly, we should not change the code from core.
In this case, we can create a new plugin in your home module:

Code: Select all
// application/modules/home/controller/plugin/HomeRoute.php
class Home_Controllers_Plugin_HomeRoute extends Zend_Controller_Plugin_Abstract
{
   public function routeStartup(Zend_Controller_Request_Abstract $request)
   {
      Zend_Controller_Front::getInstance()->getRouter()->removeRoute('index');
   }
}


This plugin remove the route named index which is added in Bootstrap.
The final step is register the plugin with front controller. You can do it easily by adding the following line to /application/config/application.ini:

Code: Select all
[production]
resources.frontController.plugins.homeRoute = "Home_Controllers_Plugin_HomeRoute"
...


I hope this help.
Image
Nguyen Huu Phuoc, TomatoCMS's Founder
Skype: phuoc.69
Gtalk: phuoc6982
Phone: (84) 123 213 8486
Twitter: http://twitter.com/phuoc69
phuoc69
 
Posts: 335
Joined: Thu Jan 07, 2010 3:49 am

Re: Create a new module and problem of routers

Postby cakeowiec » Fri Jul 30, 2010 11:24 am

Thank you very much !
This is it what I was looking for.
cakeowiec
 
Posts: 7
Joined: Sun Jul 18, 2010 3:39 pm


Return to Modules

Who is online

Users browsing this forum: No registered users and 1 guest