CakePHP routes for index with parameter - cakephp

In my cake application, I have a referral program for salespersons. For each signup the user can pass a referral id. Normally, my website has a default route which does the following:
//www.mydomain.com -> www.mydomain.com/pages/home
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Now I want to route to another controller/action like this:
//www.mydomain.com/r:1234 -> www.mydomain.com/users/signup/r:1234
Router::connectNamed(array('r'));
Router::connect('/*', array('controller' => 'users', 'action' => 'signup'));
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
The routing for the signup works fine now, but the default route doesn't work anymore. I think the order is ok. Any ideas?

If i understood your issue than It should be look like this:
//www.mydomain.com/r:1234 -> www.mydomain.com/users/signup/r:1234
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/signup', array('controller' => 'users', 'action' => 'signup'));
Try this one.
Thanks

Related

How can I manage two different routings for a single given user in CakePHP?

For example :
Router::connect(
'/:username',
array('controller' => 'users', 'action' => 'profile'),
array('pass' => array('username'))
);
If the username parameter is prefixed by #, then it will redirect to a method. If not, it will redirect to different method.
Note : I'm using version 2.8
This should work:
Router::connect(
'/#:username',
array('controller' => 'users', 'action' => 'action1'),
array('pass' => array('username'))
);
Router::connect(
'/:username',
array('controller' => 'users', 'action' => 'action2'),
array('pass' => array('username'))
);

CakePHP - Routing with parameters not working

I'm trying to connect this:
http://example.com/activate/A1B2C3
To this:
http://example.com/users/activate/A1B2C3
Router::connect('/activate/:token', array('controller' => 'users', 'action' => 'activate'), array('pass' => 'token'));
But the parameter is not being passed along. What am I doing wrong here?
pass must be an array.
Router::connect('/activate/:token',
array('controller' => 'users', 'action' => 'activate'),
array('pass' => array('token'))
);

How to allow actions in CakePHP?

I make something horribly wrong in Cake 2.3.6 stable. I followed the Auth tutorial and added in the AppController:
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
But when I enter the site on the homepage, cake throws that I´m not authorized to access that location.
With no effect I tried in the PagesController:
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('index');
}
I double checked the tutorial and my code, there are no differences except I had to swap "$this->Post" with "$this->Calclulation" in CalculationsController.
Furthermore the login- and logout-redirects in AppController doesn´t work.
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'calculations', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => ''),
'authorize' => array('Controller')
)
);
How could it be solved? Thanks in advance :)
Please, check default routers. app/Config/routes.php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
this mean, that home page is actulay rendered by pages controller and action display,
so, you should allow display
$this->Auth->allow('display');

CakePHP reverse routing: Html link not showing correct link

Firstly, I understand there have been many questions and solutions about this but none I've found fix my issue.
In summary, the Html helper is not reverse routing correctly and instead of URLs like this:
http://website.com/user_of_website/slug_name
I get URLs like this:
http://website.com/things/view/username:user_of_website/slug:a_thing
Here is my router setup:
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
//User routes
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/register', array('controller' => 'users', 'action' => 'add'));
Router::connect('/users/:action/*', array('controller' => 'users'));
Router::connect('/things/:action/*', array('controller' => 'things'));
Router::connect(
'/:username/:slug',
array(
'controller' => 'things',
'action' => 'view'
),array(
'username' => '[a-zA-Z0-9_]+',
'slug' => '[a-zA-Z0-9_]+',
'pass'=>array(
'username',
'slug'
)
)
);
Router::connect('/:userName', array('controller' => 'things', 'action' => 'user'),array('pass'=>array('userName')));
And my Html helper:
echo $this->Html->link('View', array(
'controller' => 'things',
'action' => 'view',
'username' => 'user_of_website',
'slug' => 'a_thing'
),array(
'class' => 'text-warning'
));
I really can't see what I'm doing wrong. Any help greatly appreciated.
NB, I'm using CakePHP 2.3
Have you tried if it works if you disable the 'regular' routes for things? E.g. /things/:action/*.
My guess is that, because of the wildcard, this route will match the URL as well, and, because it is defined before your custom route, will be matched in stead.
If that resolves the problem, you may try to move that route below your 'custom' route

Cakephp routes for multilingual cms

I'm trying to create a Route in Cakephp that can have anything as a prefix. But I also want the admin route to work properly.
The prefix would in this case be a language. The Route has to link to a controller named front with action: index.
A url should look like this
www.domain.com/eng/the/rest/of/the/url_12 or
www.domain.com/nl/the/rest/of/the/url_12
This is what I have, wich means that I have to create a route for each language, and thats not what I want.
Router::connect('/', array('controller' => 'front', 'action' => 'index'));
Router::connect('/admin', array('controller' => 'cms', 'action' => 'index', 'admin' => true));
Router::connect('/nl/*', array('controller' => 'front', 'action' => 'index'));
You can use this:
Router::connect('/:i10n/:controller', array('action' => 'index'), array('i10n' => '[a-z]{2}'));
Router::connect('/:i10n/:controller/:action/*', array(), array('i10n' => '[a-z]{2}'));

Resources