Cakephp routes redirect base path - php

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.

Related

kohana 3.2.3.1 route not working when controller is in a sub folder

I've just been given a project for a CRM called Kohana, which i've never heard of until now.
Everthing works ok except for controllers that are in a sub folder and when the controller name isn't just a single word.
This controller is in system/expenses.php
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_System_Expenses extends Controller_System {
public $header = 'Expenses';
public function action_index() {
$this->template->content = view::factory('system/expenses/listings');
$this->template->content->expenses = expenses::get_all();
}
public function action_update() {
$expenses = expenses::find_by_id(form::get_value('id'));
expenses::update($expenses, form::data());
$this->redirect(request::current()->referrer());
}
}
When i try to access /system/expenses it gives a 404. If i then move the controller to the base controller folder it still doesn't work unless i rename the class to Controller_Expenses instead of Controller_System_Expenses which then works on the route /expenses
Here is the bootstrap.php file:
Route::set('automate', 'hourly')
->defaults(array('controller' => 'cron',
'action' => 'hourly'
));
Route::set('daily', 'daily')
->defaults(array('controller' => 'cron',
'action' => 'daily'
));
Route::set('auth', '<action>',
array(
'action' => '(login|logout)'
))
->defaults(array(
'controller' => 'auth',
'action' => 'login',
));
Route::set('super', 'system/(<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'system',
'controller' => 'settings',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'Dashboard',
'action' => 'index',
));
It doesn't matter if i add a route in here specifically for the url, if i remove the "super" route, nothing works in the sub folder, even if i rename the controller to Controller_Expenses inside the "system" controller folder the route i had working of just /expenses still doesn't work.
I'm at a loss of how this is supposed to work.
case sensitive directory/class name:
'directory' => 'System',
'controller' => 'Settings',
trailing slash:
'system(/<controller>(/<action>(/<id>)))'
defaults for optional parameters:
'id' => 0,

Can i hide controller and view name cake php 2?

I am using cake php and due to some reason i want to hide controller and action name from the url . current url us like
http://192.168.1.31/home/this_is_test
where home is controller name and this_is_test is slug which is dynamic . i want the url like
http://192.168.1.31/this_is_test.
my routes.php is
Router::connect('/', array('controller' => 'home', 'action' => 'index'));
Router::connect('/dashboard', array('controller' => 'dashboard', 'action' => 'index'));
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/admin/login', array('controller' => 'users', 'action' => 'login', 'admin' => true));
Router::connect('/contents/*', array('controller' => 'contents', 'action' => 'view'));
Router::connect('/home/*', array('controller' => 'Home', 'action' => 'index'));
I have read a couple of solution after googling . also tried this in routes.php . but no luck
Router::connect(
'/:query',
array('controller' => 'Home', 'action' => 'index',1),
array('query' => '[a-zA-Z]+')
);
anybody have idea about this if it is possible??
Your solution
For static text try this:
Router::connect('/this_is_test', array(
'controller' => 'home',
'action' => 'this_is_test OR any_other action name'
));
If it's dynamic
Router::connect('/:id',
array('controller' => 'home', 'action' => 'index'),
array(
'pass' => array('id'),
array('id' => '[A-Za-z]')
)
);
References: Cakephp2.x Route
I hope I knew what you really want to achieve. You can place the Route in the last position. Here is the reference .
Other option would be to use alias for your controller. So you call your controller some thing else and set a new name for your controller then call it in you Route.
If this doesn't work then you would need to write a bespoke Component in order to help you to do that.

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

Different view route in CakePHP

I can somehow change the generated and accepted routes for simple urls, in the routes.php:
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/register', array('controller' => 'users', 'action' => 'add'));
This works like a charm. However, this doesn't:
Router::connect('/eintrag/:id', array('controller' => 'entries', 'action' => 'view'));
Router::connect('/bearbeiten/:id', array('controller' => 'entries', 'action' => 'edit'));
When I try to get a route for this, via echo $this->Html->url(array('controller' => 'entries', 'action' => 'view', $entry['id'])), I get /entries/view/1. And the url /eintrag/1 is not accepted by the router.
How can I prettify my view and edit routes like I can do with parameterless routes?
You need to use a third param in your route, as you are passing it :id specifically.
// SomeController.php
public function view($id = null) {
// some code here...
}
// routes.php
Router::connect(
'/eintrag/:id', // e.g. /eintrag/1
array('controller' => 'entries', 'action' => 'view'),
array(
// this will map ":id" to $id in your action
'pass' => array('id'),
'id' => '[0-9]+'
)
);
should do it.
More info # the cookbook
$this->Html->url() is just a Helper Function, that simply generates a URL according to paramaters passed, however when you actually open this Url then it will route request made for /eintrag/1 to /entries/view/1

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