CakePHP: pagination and custom routes - cakephp

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.

Related

make custom url pagination in cakephp

i m new in cakephp
first time load url like this
http://domain.com/td/city
http://domain.com/td/ is a static
city is a dynamic
in pagination the url display like
http://domain.com/controller/action/city/page:2
but i m want url like this in pagination
http://domain.com/td/city/2
please help me to solve this
UPDATE:
i don't want "controller", "action" and "page:" keyword in url
my routes define is
Router::Connect('/td/:city/*',
array('controller' => 'properties', 'action' => 'citybasedproperties' ),
array('city' => '[a-z0-9-]+', // regex again to ensure a valid city or 404
'pass' => array('city') // I just want to pass through city to my controller
));
http://www.website.com/post/page:2
we would like to change it to
http://www.website.com/post/page/2
1. /app/Config/routes.php
Add or modify the existing route to
Router::connect('/post/page/:page', array(
'controller' => 'post',
'action' => 'index'
), array(
'pass' => array(
'page'
),
'page' => '[\d]+'
));
2. /app/Controller/PostsController.php
Add or modify the existing controller to
public function index($page = 1) {
// ...
$this->request->params['named']['page'] = $page;
// ...
}
3. /app/View/Posts/index.ctp
Add or modify the existing view to
$paginator->options(array(
'url'=> array(
'controller' => 'post',
'action' => 'index'
)));
You should read this post SEO Friendly URL in CakePHP Pagination
Read the Routing chapter of the documentation it covers that case with an example and explains how routing in CakePHP works. I suggest you to actually read and try to understand the whole page and not just copy and paste the examples.

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 same action, different route for different params

I want to connect the following routes:
register/about_you
my_profile/edit/about_you
...to the same action. But I want the second route to bring in a parameter that I can use to identify that we are on the edit page, not registration.
I currently have in my routes.php:
Router::connect('/register/:action/*',
array(
'controller' => 'registration'
)
);
Router::connect('/my_profile/edit/:action/*',
array(
'controller' => 'registration',
'edit' => 1
)
);
This works to connect the URLs above to the correct place - and I can use $this->params['edit'] in the controller. However, when I try to build a link, using
Router::url(array('controller' => 'registration', 'action' => 'about_you', 'edit' => 1, $i));
I get
/register/about_you/0/edit:1
Instead of
/my_profile/edit/about_you
What am I doing wrong?
Did you try interchanging it like this?
Router::connect('/my_profile/edit/:action/*',
array(
'controller' => 'registration',
'edit' => 1
)
);
Router::connect('/register/:action/*',
array(
'controller' => 'registration'
)
);
If it is like this, you'll get /my_profile/edit/about_you/0

cakephp named parameters break REST based web service

I have a json REST based in the form of:
Router::mapResources('Test');
Which is equivalent to for the index method to:
Router::connect( '/Test',
array(
'controller' => 'ChannelSources',
'action' => 'index',
'[method]' => 'GET' ),
array();
I am trying to add support for named parameters this method.
but apparently its breaks the Router method as an index action is not part of the URL
i have tried using
Router::connectNamed(array('somenameparam'));
But it failed.
I would create a specific route so that you can pass in the right parameters, then you can pass your params into the route.
Have a look at, http://book.cakephp.org/2.0/en/development/routing.html#passing-parameters-to-action
Router::connect(
'/blog/:id-:slug', // E.g. /blog/3-CakePHP_Rocks
array('controller' => 'blog', 'action' => 'view'),
array(
// order matters since this will simply map ":id" to $articleId in your action
'pass' => array('id', 'slug'),
'id' => '[0-9]+'
)
);

How to route correctly when an argument is passed in a function?

I have a route:
Router::connect('/restaurants/*', array('controller'=>'restaurants', 'action' => 'view'));
that when a user accesses site.com/restaurants/Seafood, they get a list of seafood restaurants. Well, the problem is, now I want to add an edit function in my controller, and site.com/restaurants/edit/4 routes to the view function of my controller. How do I tell my routes to send /restaurants/edit to the edit() function?
I understand after the fact that the greedy star was a bad idea, but I didn't know how to make my function for view() work correctly without it. Here is my view code:
public function view($type=null) {
$this->set('title', $type.' restaurants in and near Gulf Shores');
$this->paginate['Restaurant']=array(
'limit'=>9,
'order'=>array(
'id'=>'asc'
),
'joins' => array(
array(
'table' => 'cuisines_restaurants',
'alias' => 'CuisinesRestaurant',
'type' => 'inner',
'conditions'=> array('CuisinesRestaurant.restaurant_id = Restaurant.id')
),
array(
'table' => 'cuisines',
'alias' => 'Cuisine',
'type' => 'inner',
'conditions'=> array(
'Cuisine.id = CuisinesRestaurant.cuisine_id'
)
)
)
);
$this->set('restaurantType',$this->paginate($this->Restaurant, array('cuisine_type'=>$type)));
}
This is the proper way to do routings like that:
Router::connect(
'/restaurants/:type',
array('controller'=>'restaurants', 'action' => 'view'),
array(
'pass'=>array('type'),
'type'=>'regexHere'
)
);
Router::connect(
'/restaurants/edit/:id',
array('controller'=>'restaurants', 'action' => 'view'),
array(
'pass'=>array('id'),
'id'=>'[0-9]+'
)
);
Another bright side to this way is that you can route according to the regular expression, so if someone tries to access yourwebsite/restaurants/edit/notanumber won't be routed into the edit page.
If you have a low number of controllers where you need to implement this functionality, you can do it the "quick 'n dirty" way, i.e. explicit routes:
Router::connect('/restaurants/edit/*', array('controller'=>'restaurants', 'action' => 'edit'));
(make sure to put this line above your greedy one in routes.php)
If you need this functionality for many controllers and actions, then more versatile routing would make more sense.

Resources