How to remove controller and action name from URL in CakePHP? - cakephp

How can I write working router:connect to have SEO friendly links?
In my site, I have articles and category. In right sidebar article's category is listed with below link.
<?php echo $this->Html->link(ucwords($data['Category']['name']),array('controller'=>'Articles','action'=>'displayArticles','cat'=>$data['Category']['name'])) ?>
This gives me link like - http://example.com/Articles/displayArticles/category-name , now I want the link as http://example.com/category-name. So for that I have tried below code but its not working.
Router::connect(
'/:query/*', array('controller' => 'Articles', 'action' => 'displayArticles'), array(
'params' => array('query', 'cat'),
'named' => array(
'query', 'cat'
)
)
);
So please someone let me know, how to achieve just category name(parameter) in URL.
Thanks in advance!

Router::connect(
'/:query',
array('controller' => 'Articles', 'action' => 'displayArticles',1)
array('query' => '[a-zA-Z]+')
);
Here id is numeric with regex.
please see this
You will also have to give parameter count in router.

Related

CakePHP routing to controller and page

Is it possible to identify a controller and pagename in the same url
Router::connect('/:controller/');
Router::connect('/:pagename/', array('controller' => 'home','action'=>'index'));
www.example.com/controller
so that the controller goes to the :controller/index
www.example.com/pagename
so that the page goes to home/index
Its really confusing what really you wants. If a url ends with :controller like www.example.com/posts its generally map index action. Now if the url ends with pagename, means action, like www.example.com/mypage, you can map that as-
Router::connect('/mypage', array('controller' => 'homes', 'action' => 'index'));
So when a user browse www.example.com/mypage, it will map to HomesController and index action.
Try following code for pagename:
Router::connect('/:slug', array('controller' => 'home', 'action' => 'index'));
UPDATE 1
If you want to treat www.example.com/user as same as www.example.com/users then you need to add following, because CakePHP controller name is plural as per CakePHP naming convention:
Router::connect('/user/:action/*', array('controller' => 'users'));
UPDATE 2
Router::connect(':slug') overwrite all of default routing. So you need to use custom routing class as like as :
App::uses('SlugRoute', 'Routing/Route');
Router::connect(
'/:slug',
array('controller' => 'home', 'action' => 'index'),
array('routeClass' => 'SlugRoute')
);

How to redirect in CakePHP with # in url, and not %23?

I am using CakePHP and I am doing this:
$this->redirect(array('controller' => 'users', 'action' => 'view',$id));
output in the browser: .../users/view/42
Since I am using JQueryUI tabs, I want the user to be redirected to the tab he just edited, so it should looks something like :
$this->redirect(array('controller' => 'users', 'action' => 'view',$id."#groups"));
output in the browser: .../users/view/42%23groups
But expected result: .../users/view/42#groups
Q: How to send a correct url with #id in it to send the focus ?
I want, if possible, not use a custom GET param that echo a js in the view.ctp to get the focus asked.
I hope it is a CakePHP issue and don't need to change some .htaccess like (How to rewrite a URL with %23 in it?)
Please read the documentation
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::url
$this->redirect(array(
'controller' => 'users', 'action' => 'view', $id, '#' => 'groups'));
This is how I achieved it:
// Redirect to the home page.
$this->redirect(array(
'controller' => 'cars',
'action' => 'index',
'#' => 'thankyou'
));

How to display name in the url instead id in Cakephp?

I have created an application in cakephp environment,
I am displaying the user profile using user_id in user profile page
I am sending the value to controller using anchor tag like below:
(I can't send the name because it is not unique).
View Profile
Function in User Controller :
function viewprofile($userid){
// my logic
}
the profile url is like this:
http://www.xyz.com/users/12
I want to display the name of user instead user_id
How can we display name(with slug) in url in cakephp
Please help me
echo $this->Html->link('View Profile', array(
'controller' => 'users',
'action'=> 'view',
$result['user']['slug']
));
you have to do 3 things.
Create unique User Slugs (you might have that already ;-) )
Create your links with the code Dave mentioned before
echo $this->Html->link('View Profile', array('controller' => 'users','action'=> 'view',$result['user']['slug']));
tell your routing what to do (/app/condig/routes.php) with it
Router::connect('/user/*', array('controller' => 'users', 'action' => 'view'));
Now you will get pretty urls and the slug will be a parameter that can be used to find the user in your User table
Try this code
echo $this->Html->link('View Profile',array('plugin' => false,
'controller' => 'users',
'action' => 'view',
"slug"=>$data['slug']));
And in your Routes file
Router::connectNamed(array('slug'));
Router::connect('/user/:slug', array(
'plugin' => false,
'controller' => 'users',
'action' => 'view',
),array(
"pass"=>array("slug")
)
);

CakePHP: $this->set(...) is empty in view?

I can't seem to figure out how to use routing parameters and the set-function. Here is what I did:
Config/routes.php:
Router::connect('/professions/:linkname', array('controller' => 'professions', 'action' => 'display'));
Controller/ProfessionsController.php:
public function display($linkname = null) {
$this->set('test', $linkname);
//$this->set('profession', $this->findByLinkname($linkname));
}
View/Professions/display.ctp:
<?php echo $test; ?>
when I open /professions/test, the page is empty. Why? (As you can see, $test is only a test. I commented out what I really intended to do. But that is not working either)
Ok, RFTM... found it in the official book. Passing the parameters has to be specified like so:
Router::connect('/professions/:linkname', array('controller' => 'professions', 'action' => 'display'), array('pass' => array('linkname'));
Now it works.
It explains in detail how to do what you're asking in the CakePHP Book here:
http://book.cakephp.org/2.0/en/development/routing.html#passing-parameters-to-action
Or, per Eike's answer (but in a less rude way), try this:
Router::connect(
'/professions/:linkname',
array(
'controller' => 'professions',
'action' => 'display'
),
array(
'pass' => array(
'linkname'
)
)
);
Obviously you could put this all in one line if that's easier or if it's more to your liking.

CakePHP: pagination and custom routes

I don't seem to be able to use a custom route with pagination. The URL of the blog should be http://www.domain.com/en/page:2. However, the links generated by the PaginateHelper (prev and next), keep adding the controller and action, so that the URL looks like http://www.domain.com/posts/index/en/page:2.
The route config is quite simple:
Router::connect(
'/:lang/*',
array(
'controller' => 'posts',
'action' => 'index'
),
array(
'lang' => '[a-z]{2}',
'pass' => array(
'lang'
)
)
);
I set this in the view:
$paginator->options(
array(
'url' => $this->passedArgs
)
);
and also to set the path manually not using an array
this happens with Cake 1.33
Any help would be greatly appreciated!
It seems prev and next method of Paginator helper doesn't use default options. That's why
$paginator->options(
array(
'url' => $this->passedArgs
)
);
doesn't work. You can set it on prev and next method directly. For example:
$paginator->prev('<< Previous', array('url' => $this->passedArgs));
Hope that help.

Resources