Zend Routing: Route to route ID - php

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+'
)
);

Related

Cakephp routes redirect base path

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.

Kohana 3.2: Route the control to another path than default

I have tried the following way
Route::set('sections', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(admin|affiliate)'
))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
Instead of Home controller in Campaign folder I need to load Home Controller from Campaign/City folder by default. I have used the above code in bootstrap.php, but it gives 'URL not found on this server' error
The array that is passed as the third parameter to Route::set() restricts the values that can be passed to the route. In your code array('directory' => '(admin|affiliate)') restricts the directory parameter to be either 'admin' or 'affiliate' To have it go deeper you would need to modify the route.
The Kohana Routing Guide has a bunch of examples using filters to route in any way you could possibly imagine, but you could route to subdirectories without turning to filters.
For example, with the following directory structure:
classes/Controller/
Admin/
Cupertino/
Home.php (Controller_Admin_Cupertino_Home)
Home.php (Controller_Admin_Home)
Affiliate/
Cupertino/
Home.php (Controller_Affiliate_Cupertino_Home)
Home.php (Controller_Affiliate_Home)
And the following route:
Route::set('sections', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(admin/cupertino|admin|affiliate/cupertino|affiliate)'
))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
The URLs index.php/admin, index.php/admin/cupertino, index.php/affiliate, and index.php/affiliate/cupertino will route through their respective controllers.
Subdirectories need to be listed before their parents otherwise Kohana will always match to the parent. e.g. the following will always route to Controller_Admin_Home even for the URL index.php/admin/cupertino:
`array('directory' => 'admin|admin/cupertino')`.
Using filters might look something like the following:
Route::set('admin_subsections', 'admin/<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(cupertino|sanjose|santacruz)'
))
->filter(function($route, $params, $request)
{
// append "admin/" to the directory param
$params['directory'] = 'admin/' . $params['directory'];
return $params; // Returning an array will replace the parameters
})
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
Route::set('sections', '<directory>(/<controller>(/<action>(/<id>)))',
array(
'directory' => '(admin)'
))
->defaults(array(
'controller' => 'home',
'action' => 'index',
));
And again, order matters.

Admin Routing in cakephp

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.

CakePHP - Routing Using 'admin_' Prefix

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

CakePHP route with regex

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.

Categories