I am using CakePHP 2.6.9.
I want to do following:
www.example.com/detail/10 should refer to controller => frontends and action => detail
www.example.com/admins/login should refer to controller => admins and action => login
I edited routes.php as follows:
Router::connect('/:action/*',
array('controller' => 'frontends', 'action' =>'detail'));
But when I try www.example.com/admins/login it shows the following error:
The action admins is not defined in controller FrontendsController
It proves that www.example.com/admins/login refers to
Router::connect('/:action/*',
array('controller' => 'frontends', 'action' =>'detail'));
Routing. I want
Router::connect('/:action/*',
array('controller' => 'frontends', 'action' =>'detail'));
will be only for controller => frontends and action=>detail, rest of url will work as default. Any idea?
this will do want you want.
Router::connect('/detail/*', array('controller' => 'Frontends', 'action' =>'detail'));
Mostly cakephp urls are like /controller/action/id. Your template of the route /:action/* tells that you are not using controller names in urls instead you are using only action names like /detail/id and /admins/id, and all actions are in Frontends controller. You can see from the error message that it tried to find admins action in Frontends conntroller.
Related
I am using cakephp 3. I want to hide frontends controller in url.
My Routes config:
Router::connect('/:action', array('controller' => 'frontends'));
And I want to refer all function to bloggers controller when url start as www.example.com/bloggers
Router::connect('/bloggers/:action', array('controller' => 'bloggers'));
But www.example.com/bloggers also refers to frontends Controller's index function. It should refer to bloggers Controller's index function. Any help?
Just change the order of your routing
First write this
Router::connect('/bloggers/:action', array('controller' => 'bloggers'));
and then this one
Router::connect('/:action', array('controller' => 'frontends'));
I want to remove controller name from url. It works for one controller but it does not work for more than one controller. Here is my code in Route.php:
Router::connect('videos/:action', array('controller' => 'videos'));
Router::connect('/:action', array('controller' => 'frontends'));
but when I try to access http://local.tbn24.dev/videos it shows:
Error: The action videos is not defined in controller FrontendsController
which proves the above url refer
Router::connect('/:action', array('controller' => 'frontends'));
I want this url to reach the videos controller index function. How could I use both Route::connect() configuration?
but when I try to access http://local.tbn24.dev/videos
There is no route for that
Of the two routes defined, this one does not match the above url as it is only one path segment:
Router::connect('videos/:action', array('controller' => 'videos'));
Therefore, it'll match the catch all route, with videos being interpretted as the action to look for.
Also note that without a leading slash, the route won't match any request as they will always start with a leading slash.
A route to match only a controller name
To define a route to match /videos - either define a route to match that specific string:
Router::connect('/videos', array('controller' => 'videos', 'action' => 'index'));
Or, define a route with a restrictive pattern:
Router::connect(
'/:controller',
array('action' => 'index'),
array('controller' => 'videos|stuff'),
);
For more information on routes, check the documentation for the version of CakePHP you are using.
I want to remove deal/deals for all the actions of deals controller:
http://example.com/deal/deals/voucher/dsfsdfdf should be http://example.com/voucher/cPH5aGr1
etc.
You should define you routes in you app/config/routes.php
Router::connect('/voucher/*', array('controller' => 'Deals', 'action' => 'voucher', 'plugin' => 'Deal'));
I'm using cakephp 2.3.8 and I want to create custom content for my homepage. I'm having trouble getting anything from the Pages Controller to the home.ctp view
In my Pages Controller I've created a home function :
public function home() {
$test = 'test';
$this->set(compact('test'));
}
But when I go to home and check with the debug kit I can see there is no $test variable.
How do I get information from the controller to the view for the home page?
My guess is that the default route for the pages controller is causing you trouble:
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
So when you go to /pages/home, it thinks it's got to execute the 'display' action still.
Try putting a route above it like this:
Router::connect('/pages/home', array('controller' => 'pages', 'action' => 'home'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
That way, the home page will render your home action, and all others will render the default 'display' action.
That's all in /app/config/routes.php.
I'm developing a simple CMS in CakePHP, right now it has 4 controllers in it(Menus,Site,Roles,Users), I want to rewrite one controller, but I'm having problem.
I use all the actions only as admins for admin purpose like admin_view, admin_add......
except siteController(this controller is only for frontend purpose)
I need my www.example.com/site/view/something_here must be replaced to www.example.com/something_here - this will be displayed in front-end so.
I added a line in my routes file:
Router::connect('/*', array('controller' => 'site', 'action' => 'view'));
But after adding this I couldn't able to use other controllers.
I again added some more lines before the above line:
Router::connect('/admin/Menus/*', array('controller' => 'menus', 'prefix' => 'admin'));
Same for all other controllers, but if I send any action or id in url it doesn't works.
like - http://www.exmple.com/admin/menus/[view/1] - the one inside square bracket doesn't works.
any Ideas on rewriting this?
I just answered a similar question on another thread.
To put the admin controller routes before the '/*'-route was the right idea, but the way you did it the router can't assign an action. You could use the following for each controller:
Router::connect('/admin/Menus/:action/*', array('controller' => 'menus', 'prefix' => 'admin'));
Or you could use the default prefix-routing routes, so you don't have to add a route for each new controller.
// prefix routing default routes with admin prefix
Router::connect("/admin/:controller", array('action' => 'index', 'prefix' => 'admin', 'admin' => true));
Router::connect("/admin/:controller/:action/*", array('prefix' => 'admin', 'admin' => true));