How to display name in the url instead id in Cakephp? - 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")
)
);

Related

How to remove controller and action name from URL in 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.

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

CakePHP Routing: Changing pretty URLs site-wide

Let's say I want a page to have a pretty URL:
Config/routes.php
Router::connect('/profile', array('controller' => 'users', 'action' => 'profile'));
If I want to send visitors to that page, I can use the URL like:
$this->redirect('/profile');
$this->Html->link('Your Profile', '/profile');
But let's say I change my mind and I now want the URL to be:
/account
How can I change the entire site without changing every instance of /profile to /account?
Alternatively...
Another way to ask my question would be how can I properly code all URLs using Cake array syntax (which I prefer to do rather than hardcode anything):
$this->redirect(array('controller' => 'users', 'action' => 'profile'));
$this->Html->link('Your Profile', array('controller' => 'users', 'action' => 'profile'));
And then ensure that any time that controller/action combination is called, it sends people to the URL:
/profile
And have this rule in one place that can be changed. Something like:
Router::connect(array('controller' => 'users', 'action' => 'profile'), '/profile');
// Later change to
Router::connect(array('controller' => 'users', 'action' => 'profile'), '/account');
Is there any way to do that, and also allow further request parameters to be passed along to be added to the URL?
Have a look at the routing documentation: http://book.cakephp.org/2.0/en/development/routing.html
In your app/routes.php add:
Router::connect('/profile', array('controller' => 'users', 'action' => 'profile'));
Now you can create your links like this:
echo $this->Html->link('Link to profile', array('controller' => 'users', 'action' => 'profile'));
Or if you want to allow additional parameters:
// When somebody comes along without parameters ...
Router::connect('/profile', array('controller' => 'users', 'action' => 'profile'));
// When somebody parses parameters
Router::connect('/profile/*', array('controller' => 'users', 'action' => 'profile'));
And then you will be able to do something like:
$userId = 12;
echo $this->Html->link('Link to other profile', array('controller' => 'users', 'action' => 'profile', $userId));
The $userId will then be available in the controller via:
echo $this->request->params['pass'][0];
// output: 12
This way you can easily change the url's of your website without having to change every single view/redirect or what so ever. Please bare in mind that you should not change your controller names! Because that will mess up a lot. Choose wisely ;-)
http://book.cakephp.org/2.0/en/development/routing.html
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html

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

CAKEPHP - how to leave the plug in url

I have a ACL plugin, and I want to be able to redirect back to users/index from this plug in.. but I end up getting funny url's that don't exist. such as /cakephp/admin/acl/users/index
how can I make it go to cakephp/users/index
I have looked through the HTML helper and I'm stumped.
I will be in the cakephp chat room as well
Thanks
You can 'reset' the plugin by setting it to 'false' when creating a link;
e.g.;
echo $this->Html->link('go to user overview', array(
'controller' => 'users',
'action' => 'index',
'plugin' => false
);
update
My guess is that you're using both prefix routing and a plugin. To reset both the plugin and the prefix, do this;
echo $this->Html->link('go to user overview', array(
'controller' => 'users',
'action' => 'index',
'plugin' => false, // resets the plugin
'admin' => false, // resets the admin-prefix
);

Resources