I have created a route which looks like this
Router::connect('/:slug', array('controller' => 'categories', 'action' => 'view'),
array('pass' => array('slug')));
Until here, everything works okey, visiting the link http://example.com/animals-and-pets, works perfect.
On this page I have a pagination and this gives me e big problem, the links for the pages, are generating wrong, like this: http://example.com/categories/view/animals-and-pets/page:2.
The result that I want to obtain is example.com/animals-and-pets/2.
Thanks for your help in advance!
I once did it this way:
change CakePhp1.3 paginator destination url?
However, it could get much easier if you use \page:2 instead of \2
Now in cake 2.0 I call the $this->Paginator->options() to set the correct url in the view before the rest of the pagination options. Something like:
//Set the correct url for the pagination, cake will add the "page:" and "sort:" variables to this url
$this->Paginator->options(array('url'=> array('controller' => 'categories', 'action' => 'view', 'slug' => $this->params['pass'][0])));
//now display the pagination
echo $this->Paginator->counter(array('format' => __('Page {:page} of {:pages}')));
echo $this->Paginator->prev('«', array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next('»', array(), null, array('class' => 'next disabled'));
Hope this helps
Related
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.
I am facing some problem with rewriting cakephp urls
Here is my url:
domain.com/users/members/mygroup
domain.com/users/admins/mygroup
I want to rewrite this to
domain.com/mygroup/members
domain.com/mygroup/admins
I have tried the following code but it's not working
In the routes.php i have created the following routes
Router::connect('/:groupname/:members', array('controller' => 'users', 'action' => 'members'),array('pass' => array('groupname')));
Router::connect('/:groupname/:admins', array('controller' => 'users', 'action' => 'admins'),array('pass' => array('groupname')));
Here is the links:
<?php echo $this->html->link('Members',array('controller'=>'users','action'=>'members','groupname'=>$groupdata['Group']['group_slug'],'members'=>members),array('escape'=>false,'class'=>'links','id'=>'memlink'));?>
<?php echo $this->html->link('Admin',array('controller'=>'users','action'=>'admins','groupname'=>$groupdata['Group']['group_slug'],'admins'=>admins),array('escape'=>false,'class'=>'links','id'=>'admnlink'));?>
When i create routes like this the first routing i.e member routing is working fine, but the second routing admin is not working, it's picking the members action and executing the members method but the url appears correct, only the action is wrong.
How can i resolve this.
Try this.
Router::connect('/:groupname/:members', array('controller' => 'users', 'action' => 'members'),array('pass' => array('groupname', 'members')));
Router::connect('/:groupname/:admins', array('controller' => 'users', 'action' => 'admins'),array('pass' => array('groupname', 'admins')));
Update
Router::connect('/:groupname/:action', array('controller' => 'users'),array('pass' => array('groupname')));
Router::connect('/:groupname/:action', array('controller' => 'users'),array('pass' => array('groupname')));
working fine for me, put the code like this,
echo $this->html->link('Members',array(
'controller'=>'users','action'=>'members',
'groupname'=> 'mygroup'), array(
'escape'=>false,'class'=>'links','id'=>'memlink'
));
echo $this->html->link('Admin',array(
'controller'=>'users','action'=>'admins',
'groupname'=> 'mygroup'),array(
'escape'=>false,
'class'=>'links','id'=>'admnlink'
));
I am new in php and cakephp. Just trying to make simple navigation menu.
<li><?php
//pr($this->Html-);
echo $this->Html->link('Entertainment', array(
'controller' => 'UpcommingEntertainments',
'action' => 'index'
));
?></li>
its works fine if I am at www.example.com. The problem is if I am at /admin/* and i click this link, it takes me to www.example.com/admin/Entertainment, I want to go to www.wxample.com/Entertainment.
What should be my link code?
Please try:
echo $this->Html->link('Entertainment', array(
'controller' => 'UpcommingEntertainments',
'action' => 'index',
'admin' => false, // thats what you need
'plugin' => false, // could be helpful if you plan using plugins
));
I included the plugin parameter, because you could encounter the same issue, if you use plugins.
Hope that helps.
<li><?php
//pr($this->Html-);
echo $this->Html->link('Entertainment', array(
'admin' => false, // add this
'controller' => 'UpcommingEntertainments',
'action' => 'index'
));
?></li>
There is a good answer here offering a bit more indepth explanation.
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")
)
);
I have configured slugs for all my posts and I need the Router that will do the links like:
/controller/post_slug_name
and I need this for all the controllers, but when I go with:
Router::connect('/admin', array('admin' => true, 'controller' => 'settings', 'action' => 'dashboard'));
Router::connect('/:controller/:slug', array('action' => 'index'), array('pass' => array('slug')));
The admin panel is not working. How can I make it like this, simple, and with admin panel working? Thanks
EDIT:
With those tree routers is working as I would like, except that in the control panel I am getting even the index actions in the urls and not cool
Router::connect('/admin', array('admin' => true, 'controller' => 'settings', 'action' => 'dashboard'));
Router::connect('/admin/:controller/:action/*', array('admin' => true));
Router::connect('/:controller/:slug', array('action' => 'index'), array('pass' => array('slug')));
I have not tried this so I am not sure it works, but please try the following...
Router::connect('/:controller/:slug', array('action' => 'view:slug'));
You view function also needs to accept $slug as paramenter:
function view($slug){
...
}
If the above does not work, you could try this as well:
Router::connect('/:controller/*',array('action' => 'view'));
Once again, I have not tried any of these codes and dont know if any works, just putting ideas outthere. When I get home I will them.
Thanks,
try connecting /admin/* instead of just /admin