cakephp reverse routing conflicts with pagination logic - cakephp

I've a new question :)
I'll briefly explain what I'm trying to achieve. Right now I have an url that looks like this.
/products/index/brand:figleaves
I want this to look like this
/brand/figleaves
By writing the following route rule I get what I want.
Router::connect('/brand/:brand/*', array('controller' => 'products', 'action' => 'index'));
Everything goes fine, but then I discovered the pagination logic has been destructed.
If I click on 'next page' I get redirected to the url /products/index/page:2.
it doesn't pass the brand parameter
it redirects back to the products_controller and not to the url I defined in the route rule.
In fact I'd need this as url /brand/figleaves/page:2.
Strange thing is if I browse to /products/index/brand:figleaves and click on Next, then I get redirected to /brand/figleaves/page:2. How can this be explained?
I'd appreciate some help with this :)
Kind Regards,
Laurent

For those interested in how I solved this.
I just defined some options in the paginator in my view and passed the value explicitly, like this.
$this->Paginator->options(array
('url'=> array(
'controller' => 'products',
'action' => 'index',
'brand'=>$this->params['brand']
)));
That does the job :)

Related

customize routing cakephp 2.X

i'm having troubles with my routes.php file, in the past my applcation works fine with the routes like this:
Router::connect( '/imprenta_online/:family/:subfamily/:id/:title/:quantity_id/:description/:days/*',
array('controller' => 'imprenta_online', 'action' => 'home'),
array('pass' => array('slug','slug', 'id','slug', 'slug', 'quantity_id', 'slug', 'slug')
));
but now, the SEO manager needs to remove the controller from the url, i'm trying to do it in the routes.php and, first, this change works for THIS URLS, but all the routes that uses the prefix backend (backend_index, backend_edit...) show me errors on paginate...
I'm not "good" whit this, so i'd like to know what can i do???
I need some help to know which changes i need to do, all the others routes works by default with cake routes, so i didn't have to changes them.

CakePHP customize Router URL order

I'm using CakePHP to one of my project. When I was designing the URL, I know CakePHP does well when I use
$this->Html->Link('Add Post', array('controller'=>'posts', 'action'=>'edit', 1234))
CakePHP will generate URI /appname/posts/edit/1234. But what I'm thinking /appname/post/1234/edit is better than the former one. I couldn't find a solution to workaround with it.
I've tried
$this->Html->Link('Add Post', array('controller'=>'posts', 1234, 'action'=>'edit'))
but CakePHP will ignore the order of array.
Does anyone know how to achieve this URL? Basically I don't want to modify function Router::url, what I need is to write code in my ctp view file and CakePHP to know the right order I put. How can I do that?
BTW, I'm using CakePHP 2.3.
This is a job for routing, more specifically reverse routing : http://book.cakephp.org/2.0/en/development/routing.html
In app/Config/routes.php you could add something along the lines of the following:
Router::connect(
'/:controller/:id/:action',
array('controller' => 'posts', 'action' => 'edit'),
array('id' => '[0-9]+')
);
Here, you tell Cake to reverse route any request coming from the edit method of the posts controller by defining the order of pre-route named parameters. In your view, the link should be constructed like this:
echo $this->Html->link('Add Post', array(
'controller' => 'posts',
'action' => 'edit',
'id' => 1234
));
Note the extra id parameter being passed that we assigned in routes.php. The Url should look like:
http://appdomain.com/posts/1234/edit
In my humble opinion, this is considered bad practice both from a conventional and SEO standpoint.

Cakephp URL masking?

For example: I have a controller: "Services" and an action called "web". Thus, my url would be:
http://www.domain.com/services/web/
How do I mask the url, such that if I type:
http://www.domain.com/servicesweb
will display exactly as http://www.domain.com/services/web/
I am reading the htaccess, not sure if its a correct solution to this.
It appears you missed the entire chapter in the CakePHP docs about Routing, which is this 'url masking' you speak of.
In your /app/config/routes.php file you will need to add this line:
Router::connect('/servicesweb', array('controller' => 'services', 'action' => 'web'));
Be sure to read the book for clarification on routing.

CakePHP route URL not found!

I am trying to do some custom routing on my site, but have been stuck for 2 days at a very silly issue. I have the following route configuration:
Router::connect('/your-solution/add-comment/*', array('controller' => 'comments', 'action' => 'add'));
Router::connect('/admin/your-solution/add-comment/*', array('controller' => 'comments', 'action' => 'add', 'admin' => true));
The problem is that when I try to load a URL formatted using the second route, it gives me a 404 not found.
The first rule works fine.
For both rules I have a separate element containing a form and pointing to a URL formatted after the respective rule. The only parameter for both actions is the solution id, which is "contained" in the wildcard.
What could possibly be the issue? Thank you very much for your help!
EDIT:
I found out another weird behaviour. When I access /admin/your-solution/add-comment/3, it goes to that action. But if I submit a form to that link, it displays a blank page, with Firebug informing me that the page was not found. Very strange...
Also, I have a similar route for editing comments. Both loading the edit form and saving the form work...
how are you?
In order to see exactly why isn't it working, go to your /app/config/core.php and seek for this line:
Configure::write('debug', 2);
And make sure the value is set to "2". This way, it'll no longer give you a 404 error, but the actual issue, since in production mode (debug set to 0), all errors are masked with a 404 error.
Let me know!
Cheers!
In your core.php be sure
Configure::write('Routing.prefixes', array('admin'));
In your comments controller, be sure you have
function admin_add() {...}
Also try other ways of formatting Routing statement.
Router::connect('/admin/your-solution/add-comment', array('controller' => 'comments', 'action' => 'add', 'admin' => true));
The order of your route is also important. You may want to check that.
For debugging which route you are using when loading the URL, try adding this code to your app_controller.php file.
function __construct() {
$route = Router::currentRoute();
pr($route);
}
These are just some tips to hopefully help you move forward.
Apparently, the problem has been lying in a disabled input. After I've deleted this element, the form submits correctly and the target page is shown.
Just for my knowledge, why didn't the form submit if it had a disabled input in it?

CakePHP custom route pagination

I would like to format my URL like so:
/news/index/page:2
to
/news/2
I would like to achieve this result by using as less code as possible (perhaps only from routes.php?), without modifying how the PaginatorHelper behaves.
Thank you for your help!
This should work
// Add this to /app/config/routes.php
Router::connect('/news/:page', array('controller' => 'news', 'action' => 'index'));

Resources