TLDR: How can I create a URL in the Controller similar to how I can use the HtmlHelper to create URLs in a View?
Problem:
I want to print the url of a controller action, in my controller (because I create my JSON string in my controller, not in a view)
In a View, I can use $this->Html->url(), but what about in a Controller?
Should I use defined constant like APP_DIR + Controller name + Controller action?)
Use the Router class.
$url = Router::url([
'controller' => 'Articles',
'action' => 'index',
'?' => ['page' => 1],
'#' => 'top'
]);
or the same thing, but in a more common/simple scenario:
$url = Router::url(['controller' => 'Articles', 'action' => 'index']);
Note: in Cake2.x, "Articles" would be lowercase.
CakePHP 2.x Router documentation
CakePHP 3.x 'Generating URLs' documentation
Related
I am using the below code in my Controller file to generate full site URL
$this->Url->build(['controller' => 'home', 'action' => 'index'], true);.
But I am getting a "Call to a member function build() on boolean" error. The reason why I am not using
Router::URL(['controller' => 'home', 'action' => 'index']);
is I don't want a relative URL. Can't I use Url builder in the controller?
I think, the main reason of your Error is wrong name of Controller.
First,Name Conventions says
"Controller class names are plural, PascalCased, and end in Controller. UsersController and ArticleCategoriesController are both examples of conventional controller names."
so your file name must be like HomesController.php. Next the Class name should be like this HomesController.
Secondly, for CakePHP character size matters. If you want build URL to your controller you have to do like this
$this->Url->build(['controller' => 'Home', 'action' => 'index'], true);
Where controller name should be capitalize.
More about building URL in CakePHP
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 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.
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 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));