searching for this question, I found this question/answer:
Kohana 3 get current controller/action/arguments
I recently switched to Kohana after using codeigniter for a while.
In Codeigniter, you could just do (in a Controller):
public function action_nameAction($param1, $param2 = null, ...){
$something = $param1;
}
by calling www.mysite.com/controllerName/param1/param2
I really liked this approach and don't really understand the point of Kohana's approach with redefining the routes...
If there is a solution to have the Codeigniter approach in Kohana, I would like some clues on how to implement it.
If not, please could someone explain me the interest of having to redefine new routes for every case that doesnt fall into the default route...
Route::set('default', '(<controller>(/<action>(/<id1>(/<id2>))))')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
$id = $request->param('id1');
$id = $request->param('id2');
Related
This question has been asked many times but for some reason the proposed solution isn't working for me.
I wan't to use names like "deleteDefaultUser" for my action. To achieve this I have done following.
Added a route
$route = new Zend_Controller_Router_Route_Static(
'user/delete-default-user',
array(
'action' => 'deleteDefaultUser',
'controller' => 'user',
'module' => 'root'
)
);
$router->addRoute('delete-default-user', $route);
Defined my action as below
public function deleteDefaultUserAction(){
//some code
}
And generated a URL like this
echo $this->url(array(), 'delete-default-user');
( This generates the URL /user/delete-default-user)
But for some reason I am still getting the error shown below:
Zend_Controller_Action_Exception: Action "deletedefaultuser" does not exist and was not trapped in __call() in C:\Users\Jay\Projects\EOP\library\Zend\Controller\Action.php on line 485
I have checked the controller and action names are correct. But from the error message it seems that Zend Framework is not applying camel case to action names.
The version of Zend Framework that I am using is 1.12.
Can any one please help with this?
Edit: If I change my action name to 'deletedefaultuser' it works correctly.
Change the action part of the route to:
'action' => 'delete-default-user',
and then it should work.
In Kohana 3.2 I'm using the default route for a simple controller/action/id setup:
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'home',
'action' => 'index',
'id' => '0',
));
Per the documentation it's pretty easy to wire up controllers and actions to a simple route like this, but route parameters (in this case id) are never accessible in the controllers.
So for example I have a route:
/user/info/123
And the controller handling that route gets called successfully:
public function action_info()
{
$id = $this->request->param('id');
echo "id=" . $id; //nothing
echo "is_null=" . is_null($this->request->param('id')); //1
}
But $this->request->param('id') is always set to null.
This seems like about the simplest example I can come up with, what could I be doing wrong here?
Turns out the company I'm working with extended Kohana with a request->param() function to do something without realizing that was already a function in Kohana, and this broke the built in functionality. Using the built in Kohana request function fixes this problem.
So this turns out to be a non-question as this is the correct way to get request parameters after all. :)
Have you tried this?
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'home',
'action' => 'index',
'id' => '\d+',
));
I understand how to create a simple custom route in the form of
example.com/archive/:year
However, I need to create a route in the form of
example.com/:make/:model
Both path components are variables. At the same time I want to keep the default Zend Framework routing. How would I go about this?
Thanks
Not for zend, but the technique is instructive and will probably work. I've struggled with this problem too, but was more for internationalisation.
http://darrenonthe.net/2011/05/06/dynamic-routing-from-database-in-codeigniter/
Basically, you cache your routes into a file, and the framework matches them to your controller/variable details. I tried lots of things, complex regexes, and in the end, this worked out really, really well. Might be a good solution for you too.
if you are using a module based file architecture, you can maintain the zend framework default routes, and add custom routes for your modules.
for example
class Example_Bootstrap extends Zend_Application_Module_Bootstrap
{
public function _initModuleRoutes()
{
$this->bootstrap('FrontController');
$frontController = $this->getResource('FrontController');
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
'modulename/:action/*',
array(
'module' => 'modulename',
'controller' => 'modulecontroller',
'action' => 'index'
)
);
$router->addRoute('routename', $route);
return $router;
}
You need to enforce some condition i.e model is integer type or something else . Like this
$route = new Zend_Controller_Router_Route(
':make/:model',
array(
'controller' => 'car',
'action' => 'logic'
),
array('make' => '\d+')
);
If you cannot distinguish them with extra condition like these then how software gone do this for you weather its action name or make ?
I have a rule defined in Kohana 3 routing but the ID parameter is not being passed to the method.
I have this in the bootstrap.php file:
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
and controller
public function action_home($id=NULL)
{
echo $id;
}
But in the following link example, the ID is usually NULL:
http://example.com/index/123
The ID should be 123 but it isn't. I don't know why.
Yeah the Ko3 routes are ridiculous.
Try removing that $id=null since K03 will assign the vars as parameters:
public function action_home()
{
echo $this->request->param('id');
}
See if that helps.
They were removed from Kohana, please see: http://dev.kohanaframework.org/issues/4357
As zombor stated:
They just don't fit into how Kohana is designed. They should not have been included in 3.0 in the first place.
Also it isn't supposed to handle all routes with the default one. You should define as many as you need.
I believe I know how to do this, but wanted to verify with my awesome community peeps. =)
Here's an example:
I have a Controller class called 'tami', with an action 'index'.
I know that if I want someone to access that controller/action combo via an URL other than "/tami/" or "/tami/index", then I should add a route, via something like this:
Route::set('pretty_tami', 'these-are-my-initials(/<action>)')
->defaults(array(
'controller' => 'tami',
'action' => 'index',
));
But, users can still access this page via /tami/.
How can I turn off the default routing, so that the only valid routes are the ones I define?
I assume I can just remove the default route found in kohana/application/bootstrap.php. Is that correct? Or would that break something else?
I'd say exactly the same as #simshaun — either remove the default route (leaving other controllers unreachable) or check in the before() function in Controller_Tami for the uri to see if it's what you're after.
If you're using Kohana 3.1, you can now use lambda logic/anonymous functions to define your routes.
Something like this would take the extra routing logic out of the controller (which is good as we're keeping it in one place):
Route::set('default', function($uri)
{
if ($uri == 'tami' OR $uri == 'tami/index')
{
// Route not allowed by the default methods
throw new Kohana_404_Exception("Route not permitted");
}
},
'(<controller>(/<action>(/<id>)))'
);
Something I haven't yet used but it looks amazingly powerful.
I think the easiest way would be to remove the default route in your bootstrap file, yes. However, any controllers that you have not manually specified a route for can no longer be accessed.
What I would do is create a class, e.g. Controller_Derouter that Controller_Tami extends. Use the before() method in Controller_Derouter to test if the controller was accessed from the default route, and if so, throw a 404. I think you should be able to do that by comparing $this->request->controller against the first URI segment.
Edit: The solution mentioned above is unnecessary if you ever only plan on disabling the default route for just the Tami controller. If that's the case, you could just implement the before() method directly in the Tami controller.
Maybe like this?
Route::set('pretty_tami', 'these-are-my-initials/<action>')
->defaults(array(
'controller' => 'tami',
));
So there wouldn't be a default action. And you probably want to update the default route (if you still have one) with a regex to exclude tami.
Route::set('default', '(<controller>(/<action>(/<id>)))', array('controller' => '/^(?!tami)/'))
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));