CakePHP Two routes at the same url - cakephp

Is it possible to create two routes at the same url?
So for example:
Router::connect('/', array('controller' => 'users', 'action' => 'login'));
Router::connect('/', array('controller' => 'home', 'action' => 'index'));
The idea is that e.g. www.mywebsite.com will show the login page as its home page without any redirects to a login page or anything. Once the user logs in then they will be taken to the home page again but instead it will load the home controller index but again same URL!
How would I do this?

Well, you definitely can't do that like that.
You could try setting / to home/index and checking whether the user is logged in in the home/index view, and display the login form if he's not. And also check for user being logged in in the controller.
That said, I really can't image why you would want to do it like that. Especially if you're using the AuthComponent.

Related

CakePHP 3.2 default redirect to the controllers' main pages

How can I specify the default pages (one page for every controller) in CakePHP 3.2 so that the cake will automatically redirect user to the page:
www.mypage/controller_name/action_name
if the user set in his browser the following url:
www.mypage/controller_name
The index action will be the landing page by default. If you want to specify a different action as the default, you can add this to routes.php
Router::connect('/', ['controller' => 'controller_name', 'action' => 'action_name']);
http://book.cakephp.org/3.0/en/development/routing.html#quick-tour

Routing in CakePHP 2.6.9

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.

Removing controller name from url does not work for more than one controller in cakephp

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.

cakephp 2 - dynamic content on the homepage

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.

CakePHP Routing query

How do I show the URL through CroogoRouter::connect function in CakePHP?
I passed the name of controller and action my routes file like:
CroogoRouter::connect('/', array('admin' => true, 'controller' => 'dashboard', 'action' => 'index'));
and I get redirected to the dashboard page, but I do not see the URL properly. I want my URL as localhost/abc/admin/dashboard and it is showing as localhost/abc only.
Since you are using the webroot (/) as route alias, it will not show the admin/dashboard bit. If you really want that (I'd discourage it if it's just for fancy display purposes), you should create a simple controller action that redirects. For example if you alter the route to this:
CroogoRouter::connect('/', array('controller' => 'dashboard', 'action' => 'home'));
And then in the DashboardsController create this action:
public function home() {
$this->autoRender = false; // We have no view, so don't render anything
$this->redirect(array('admin' => true, 'controller' => 'dashboard', 'action' => 'index'));
}
It should display the URL as you want it. Again, if it's only for display purposes (to make it look "nice") and not for any SEO kind of purpose, I'd discourage using such an ugly workaround. But it should do the trick either way.
Can you please check the controller name i think controller name should be dashboards or something.
check this link may be helpful http://bakery.cakephp.org/articles/Frank/2009/11/02/cakephp-s-routing-explained

Resources