Вопрос

I'm new to Kohana but have been reading the User Guide for about two weeks now. I am building a huge application on v3.3.1 that will have a number of different modules (i.e. residing in the modules directory). I am struggling with getting the routes to work as I need them to.

First, it's worth mentioning that I have read the User Guide about routes, modules, bootstrap etc. I am putting the route::set in the init.php file within the module, so it is being called before the "default" option in the bootstrap.php file. Here's what I have...

Directories:

application > classes > Controller > Welcome.php

modules > module1 > classes > Controller > Home.php

Routes

In module1 init.php file:

Route::set('module1', 'module1(/<controller>(/<action>))')
->defaults(array(
    'controller' => 'Home',
    'action'     => 'index',
));

In bootstrap.php file:

Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
    'controller' => 'welcome',
    'action'     => 'index',
));

When I go to localhost/index.php/module1/ it does run the Home controller of the module.

When I go to localhost/index.php it does run the Welcome controller in the application directory.

However, if I go to localhost/index.php/home it also runs the Home controller for the module. I do not want it to do this. I only want the module controller to be run when the URI is localhost/index.php/module1/controller

I know it is the default route picking up the module's controller, but I don't know how to stop it from doing this, or even if it is possible!

If anyone has experience of working with modules in this way please could you help me? You time is very much appreciated.

Many thanks, Steve

Это было полезно?

Решение

You are using the default route, which is a catch all route. What you should do, is to alter the routes in such a way that your catch all is limited to just the Controllers you have in your main application. This can be done by adding a third parameter with a regex.

Example:

Route::set('module1', 'module1(/<controller>(/<action>))')
->defaults(array(
    'controller' => 'Home',
    'action'     => 'index',
));


Route::set('main-app', '(<controller>(/<action>(/<id>)))', 
array(
    'controller' => '(welcome|login|posts)'
))
->defaults(array(
    'controller' => 'welcome',
    'action'     => 'index',
));

If I were you I would even break up the routes further and make specific routes for your Controllers.

Do however note that you cannot use the same Controller names in your main app and in the your modules. With these routes (and probably all), Kohana will always override and use the main app Controller (due to the Cascading File System), even if the route matches the module Controller.

I hope this helps and if you have further questions, just leave a comment.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top