Cake PHP route controller/action to other controller/action - cakephp

How can I make cakePHP go to ef_users/logout when I click the users/logout link?
Thanks in advance
EDIT
This doesn't seem to work
Router::connect('/users/:action/*', array('controller' => 'ef_users', 'action' => 'logout'));

A couple of options:
Make the link point to the correct place in the first place
Use routing: http://book.cakephp.org/view/945/Routes-Configuration
Redirect the user with $this->redirect( url ): http://book.cakephp.org/view/982/redirect

If in doubt, just use as precise route as possible and put it near the top of the list
Router::connect('/users/logout', array('controller' => 'ef_users', 'action' => 'logout'));

You might also consider using logoutRedirect which will still log the user out using the standard CakePHP logout function then redirect the user to your ef_users logout action.
$this->Auth->logoutRedirect = array('controller' => 'ef_users', 'action' => 'logout');
More information at: http://book.cakephp.org/view/1271/logoutRedirect

Related

Rewrite url in CakePhp

I'm new to Cakephp.
I have a home page with a lot of links like /mark-james.hmtl.../steve-pain.html, etc.
I want to display the personal page after the user clicks on the name, how can rewrite that?
Obviously now I have: Error: mark-james.hmtlController could not be found.
Router::connect(
'/user1-home-page',
array('controller' => 'users', 'action' => 'user1')
);
http://book.cakephp.org/2.0/en/development/routing.html
You can define routing as mentioned above.

Cakephp route and prefix

I have a problem with Routes in CakePHP. Let me explain.
I'm using authentication through the Auth component. I have a routing prefix called account.
When I want to edit a user, I'm calling the users controller which gives me a URL like:
/account/users/edit/5
What I want is to have a URL like:
/account/edit/5
So I changed my router like this:
Router::connect('/:prefix/edit/:id',
array('controller' => 'users', 'action' => 'edit'),
array('pass' => array('id'), 'id' => '[0-9]+')
);
which worked when I try to access /account/edit/5
My problem is located in my view. How can I access this route using the Html->link helper?
So far, I'm just doing it like this:
'/'.$this->Session->read('Auth.User.role').'/edit/'.$this->Session->read('Auth.User.id')
But it's not really clean in my opinion. I want to use the helper.
Thanks a lot for your help
Using a prefix "account" would mean needing an action like "account_edit" in your controller. That's probably not what you want. Also why put the "id" in url when it's already there in the session? Why not just use url "/account" for all users and get the id (and role if required) from session in the action?
Router::connect('/account',
array('controller' => 'users', 'action' => 'edit')
);
This would be the clean way to generate required url:
$this->Html->link('Account', array(
'controller' => 'users',
'action' => 'edit'
));
// Will return url "/account"
In general always use array form to specify url to benefit from reverse routing.
everything is just fine except router. it should be
Router::connect('/account/*',
array('controller' => 'users', 'action' => 'edit')
);
and creating anchor link in various way using Helper you can CHECK HERE

how to use i18n for one language cakephp

I need to launch a web app in spanish for the moment and I need to translate the app...
I already modified the default.po and added configure::write('Config.language', 'es') to the core.php...
what now? I don't want to add routing rigth now. Any suggestions?
PD: did everything as it is in the manual and ##$%^&%$## I cant get it to work
i18n is a tricky one to get your head around. If you're producing a website that will just be in Spanish, there is no need to use it, but I do use po messages as a matter of course, just in case.
There is a component that will help you a lot: http://bakery.cakephp.org/articles/p0windah/2007/09/12/p28n-the-top-to-bottom-persistent-internationalization-tutorial
There also used to be a script that would allow translation of slugs so that SEO would direct you to the right language. Last time I looked, it had vanished, but I'll try to piece it together for you.
For the moment, this is what I used in router.php
//route to switch locale
Router::connect('/lang/*', array('controller' => 'p28n', 'action' => 'change'));
//forgiving routes that allow users to change the lang of any page
Router::connect('/eng?/*', array(
'controller' => "p28n",
'action' => "shuntRequest",
'lang' => 'en-gb'
));
Router::connect('/ca?/*', array(
'controller' => "p28n",
'action' => "shuntRequest",
'lang' => 'cat'
));
Router::connect('/es?/*', array(
'controller' => "p28n",
'action' => "shuntRequest",
'lang' => 'es_es'
));
I'll dig around for the url translation, but it may take a while....

Routing configuration in cakephp

I am trying to implement routing in cakephp. I want the urls to mapped like this...
www.example.com/nodes/main -> www.example.com/main
www.example.com/nodes/about -> www.example.com/about
So for this I wrote in my config/routes.php file..
Router::connect('/:action', array('controller' => 'nodes'));
Now, I got the thing going but when I click on the links, the url in browser appears like
www.example.com/nodes/main
www.example.com/nodes/about
Is there some way where I can get the urls to appear the way they are routed?
Setting in .htaccess or httpd.conf would be easy - but I don't have access to that.
Regards
Vikram
This should work:
Router::connect('/main', array('controller' => 'nodes', 'action' => 'main'));
Router::connect('/about', array('controller' => 'nodes', 'action' => 'about'));
You may also do something more powerful, like this:
$actions = array('main','about');
foreach ($actions as $action){
Router::connect('/$action', array('controller' => 'nodes', 'action' => '$action'));
}
Basically if your links are created with Html helper, with the following format:
<?php echo $this->Html->link('your link', array('controller'=>'nodes', 'action'=>'main'));?>
Then the Cake will convert the links properly to www.example.com/main
But if your links are
<?php echo $this->Html->link('your link', '/nodes/main/');?>
they will point to www.example.com/nodes/main

Cakephp, Route old google search results to new home page

I have created a new website for a company and I would like all the previous search engine results to be redirected. Since there were quite a few pages and most of them where using an id I would like to use something generic instead of re-routing all the old pages.
My first thought was to do that:
Router::connect('/*', array('controller' => 'pages', 'action' => 'display', 'home'));
And put that at the very end of the routes.php file [since it is prioritized] so that all requests not validating with previous route actions would return true with this one and redirect to homepage.
However this does not work.
When I use a different path on the Router it redirects successfully. For example if I give it:
Router::connect('/*', array('controller' => 'projects', 'action' => 'browser'));
it works fine. The problem arises when the controller used is pages, action display etc.
I'm pasting my routes.php file [since it is small] hoping that someone could give me a hint:
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/company/*', array('controller' => 'articles', 'action' => 'view'));
Router::connect('/contact/*', array('controller' => 'contacts', 'action' => 'view'));
Router::connect('/lang/*', array('controller' => 'p28n', 'action' => 'change'));
Router::connect('/eng/*', array('controller' => 'p28n', 'action' => 'shuntRequest', 'lang' => 'eng'));
Router::connect('/gre/*', array('controller' => 'p28n', 'action' => 'shuntRequest', 'lang' => 'gre'));
Router::parseExtensions('xml');
Instead of trying to handle everything within the cakePHP route file, I would recommend that you use the .htaccess file to 301 redirect pages as necessary.
What you have above will not transfer the rankings over because as far as i can see there is no 301 redirect being outputted in any of the routes.php based solutions you proposed.
The bigger problem is that a Route doesn't redirect, it connects URLs with responses. In other words, it makes sure that your now invalid URLs still yield a valid page. Which is exactly the opposite of what you want to achieve.
You want to tell visitors that a URL that used to be valid isn't anymore. You do this by issuing appropriate HTTP response codes, 301 Moved Permanently in this case. Without doing this the URLs will still appear valid to search engines and they won't update their index.
You'd either have to connect all the invalid URLs via Routes to some controller action that'll issue a $this->redirect('...', 301) or you could use some .htaccess rules to redirect. Which one to use depends on the complexity of the redirect, but you'll probably be able to use simple .htaccess mod_rewrite rules.
There are enough examples on SO: https://stackoverflow.com/search?q=htaccess+301+redirect

Resources