I am currently using cake's routes config in order to set up admin views, different from that of the non-admin user. I read the routing chapter of the documentation(cake's), and stumbled upon the prefix routing. Which I thought that it is something I need to use, to accomplish what I need. So I started it with setting up the config/core.php as suggested, and uncommented this
Configure::write('Routing.prefixes', array('admin'));
Then, I added a route in the routes.php :
Router::connect('/admin', array('controller' => 'donors', 'action' => 'index', 'admin' => true));
From what I understood, with the above set, I can define a specific action for the admin, names like : admin_index or admin_view, etc. .
So my AppController has a component set like this :
public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'donors',
'action' => 'index'
),
'authError' => 'Access Denied',
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
'authorize' => array('Controller')
)
);
So when a non-admin user logs in he should be redirected to 'donors/index', and when the admin logs in I want to redirect him to 'donors/admin_index'.. How can i do this ?
I tried this :
public function beforeFilter(){
if(isset($this->params['admin'])){
$this->layout = 'stafflayout';
$this->Auth->loginRedirect = array(
'controller'=>'donors',
'action'=>'index',
'prefix'=>'admin',
'admin'=>true
);
}
And in the process of testing it out, at first glance I though it worked. but the URL does not change like 'donor/admin_index .. and am still being redirected to donors/index or equivalent, simply to /donors... Why is this not working ?
(seconndary question)Also during the process of testing this out, I changed my the controller and actions of the Auth component LoginRedirect to
'controller'=>'posts'
and
'action'=>'index'
other then 'donors', 'index', and when I logged in, I still got redirected to donors/index.. were it should have redirected me to 'posts/index'
Anyone can help me on these two issues? Primary questions is more important though!
Well the code is fine!
Router::connect('/admin', array('controller' => 'donors',
'action' => 'index', 'admin' => true));
the above will render /donors/index page whenever /admin is written in the url.
Now if you want to add prefix like /donors/admin_index then you have to create one more rule such as:
Router::connect('/donors/admin_index', array('controller' => 'donors',
'action' => 'index', 'admin' => true));
and in beforeFilter function
if(isset($this->params['admin'])){
$this->layout = 'stafflayout';
$this->Auth->loginRedirect = array(
'controller'=>'donors',
'action'=>'admin_index',
'admin'=>true
);
the above code will redirect to /donors/admin_index and routing will render /donors/index page
Related
I am trying to make an URL like these:
www.website.com/,
www.website.com
redirect to
www.website.com/members/login
through routes.php.
I have this at the moment: Router::connect('/', array('controller' => 'home', 'action' => 'index'));
How can i setup the route / to reach my desired url?
Thank you!
You can just replace the line:
Router::connect('/', array('controller' => 'home', 'action' => 'index'));
with:
Router::connect('/', array('controller' => 'members', 'action' => 'login'));
However, I believe you don't want to do this.
Just leave your routes untouched, and set up AuthComponent properly:
class AppController extends Controller {
// Pass settings in $components array
public $components = array(
'Auth' => array(
'loginAction' => array(
'controller' => 'members',
'action' => 'login',
),
//[...] rest of your Auth options
)
);
For further reference, see Cookbook 2.x: Authentication.
Probably you have AuthComponent somewhere and '/' is set as dissallowed.
$this->Auth->allow('/') should help.
I have seen many similar problems here in SO but none have helped me solving my problem.
I don't think CakePHP's AuthComponent is behaving properly when it comes to permission errors.
My CakePHP app is in a subdomain of my website (e.g. http://www.example.com/myapp). Everytime a permission error occurs (user is not allowed to see this page), Cake adds another "/myapp" in the URL (so it becomes http://www.example.com/myapp/myapp) and naturally throws an error which says that "myapp" controller doesn't exist.
In my scenario, the login view is linked to the root of the website. So, I have in my AppController:
public $components = array(
'Session',
'Auth' => array(
'loginAction' => array(
'controller' => 'users',
'action' => 'login'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
'authorize' => 'Controller',
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
);
And in routes.php:
Router::connect('/', array('controller' => 'users', 'action' => 'login'));
Router::connect('/users', array('controller' => 'users', 'action' => 'login'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
All I want is to be able to configure manually which URL Cake should redirect to in case of permission errors. Anybody knows how to do that?
I'm sorry guys, I accidentally found the answer a bit later. As CakePHP has some very intuitive labels, I started guessing some possible options for the Auth Component that would solve my problem. And I ended up discovering the unauthorizedRedirect option!
So I have:
'unauthorizedRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
And it's working just fine now. I just wish Cake's documentation had covered this... If it does, I swear I couldn't find it anywhere.
Im trying to put my app under /admin on cakephp. And i am trying to configure the admin routing. What im trying to achive is this:
Lets say the page is www.example.com so when the user type www.example.com/admin i want him/her to be redirected to the admin_dashboard.ctp (if it is logged in, otherwise redirect to log-in page). But now the problem is when i type www.example.com/admin it shows an error like:
Action PagesController::admin_index() could not be found
but if i do:
www.example.com/admin/users/dashboard it is redirected properly.
How can i achieve that? so just by typing /admin to redirect to dashboard??
and another thing is it possible to remove /users/ from url and just display admin/dashboard?
On core.php file i have added the following line:
Configure::write('Routing.prefixes', array('admin'));
And on the routes.php i have these lines :
Router::connect('/', array('controller' => 'users', 'action' => 'dashboard', 'dashboard'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/* I added this line for admin routing */
Router::connect('/admin', array('controller' => 'pages', 'action' => 'index', 'admin' => true));
In routes.php write
Router::connect('/admin', array('controller' => 'users', 'action' => 'dashboard', 'admin' => true));
instead of
Router::connect('/admin', array('controller' => 'pages', 'action' => 'index', 'admin' => true));
This redirect you to admin_dashboard when you type www.example.com/admin.
Routing prefixes are added to the action name when looking for its corresponding method in the Controller.
In the Pages Controller rename your index() method into admin_index() as suggested by the error you are getting.
Is it possible to use the Routes ID to redirect to within the controller?
For example, I predefine the login and logout URL with the id of login and logout. In my controller I determine the user needs to be logged out, can I redirect them to that route using the routes id?
Bootstrap
$router->addRoute('logout',new Zend_Controller_Router_Route('logout', array('module' => 'user', 'controller' => 'index', 'action' => 'logout')));
$router->addRoute('login', new Zend_Controller_Router_Route('login', array('module' => 'user', 'controller' => 'index', 'action' => 'login')));
Controller
return $this->_redirect('login');
Currently the above wouldn't work, Id have to use /login (aka the base URL to the route).
I had a similar requirement recently too, the way I solved it was to use the router to assemble the url, and then perform the redirect.
$redirectUrl = Zend_Controller_Front::getInstance()->getRouter()->assemble($userParams, $routeName);
$this->_redirect($redirectUrl);
See Zend_Controller_Router_Interface::assemble
From Zend Framework 1.8
$route = new Zend_Controller_Router_Route(
'index/:ident',
array(
'module' => 'user'
'controller' => 'index',
'action' => 'login'
),
array(
// match only digits
'ident' => '\d+'
)
);
I have a controller setup to accept two vars: /clients/view/var1/var2
And I want to show it as /var1/var2
SO i tried
Router::connect('/*', array('admin'=>false, 'controller' => 'clients', 'action' => 'view'));
But this stops all other controllers working as /* routes everything
All other pages that are on the site are within the admin prefix so basically i need a route that is ignored if the current prefix is admin! I tried this (regex is from Regular expression to match a line that doesn't contain a word?):
Router::connect('/:one', array('admin'=>false, 'controller' => 'clients', 'action' => 'view'), array(
'one' => '^((?!admin).*)$'
));
But I think the regex is incorrect because if i naviate to /test it asks for the tests controller, not clients
My only other routes are:
Router::connect('/admin', array('admin'=>true, 'controller' => 'clients', 'action' => 'index'));
Router::connect('/', array('admin'=>false, 'controller' => 'users', 'action' => 'login'));
What am I doing wrong? Thanks.
I misunderstood your question the first time. I tested your code and didn't get the expected result either. The reason might be that the regex parser doesn't support negative lookahead assertion. But I still think you can solve this with reordering the routes:
The CakeBook describes which routes are automatically generated if you use prefix routing. In your case these routes have to be assigned manually before the '/*'-route to catch all admin actions. Here is the code that worked for me:
// the previously defined routes
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/admin', array('controller' => 'clients', 'action' => 'index', 'admin' => true));
// 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));
// the 'handle all the rest' route, without regex
Router::connect(
'/*',
array('admin'=>false, 'controller' => 'clients', 'action' => 'view'),
array()
);
Now I get all my admin controller actions with the admin prefix and /test1/test2 gets redirected to the client controller.
I think the solution is described in the bakery article on routing - "Passing parameters to the action" (code not tested):
Router::connect(
'/clients/view/:var1/:var2/*',
array(
'controller' => 'clients',
'action' => 'view'
),
array(
'pass' => array(
'var1',
'var2'
)
)
);
The controller action would look like:
public function view($var1 = null, $var2 = null) {
// do controller stuff
}
Also you have too look at the order of your routes (read section "The order of the routes matters"). In your example the '/*' stops all other routes if it comes first, if you assign the rule after the others it handles only requests which didn't match any other route.