CakePHP custom route pagination - cakephp

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

Related

CakePHP URLs having .html extension

We are converting an exisiting HTML site into a CMS using CakePHP. Since SEO of the site has been mapped with keywords and indexed by Google the static pages i want to have urls to have the extention .html
I had a look at the Document here
But am not quite sure how to achieve this in the right way.
Any one who has worked on it can give some pointers?
simply put this line into your Router Router::parseExtensions('html');
This will tell the Router to cut off the .html as an Extension and parse what remains.
To Create correct Links to the Pages you have to give the Link() function another Parameter called "ext".
Like this:
$this->Html->link(
'Super Seo link',
array(
'controller' => 'anyController',
'action' => 'someAction',
'title' => 'seo-title-for-gods-sake',
'ext' => 'html'
) );
Have fun! Florian

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 reverse routing conflicts with pagination logic

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 :)

Is there a default controller for the index page for a CakePHP installation?

I have just successfully installed CakePHP and I see that I can edit the home.ctp view but is there a default controller for the index page?
To change the content of this page, create: APP/views/pages/home.ctp.
To change its layout, create: APP/views/layouts/default.ctp.
You can also add some CSS styles for your pages at: APP/webroot/css.
If you want to make modifications to this controller it is recommended that you copy the default
cake/libs/controller/pages_controller.php to app/controller/pages_controller.php
The reason is because you should not modify anything inside the "cake" folder where any file can be overwriten when updating your application with the latest cakephp version.
You can change the default behavior by changing the Route::connect() function arguments such as below:
Router::connect('/', array('controller' => 'requests', 'action' => 'index', 'home'));
and also if you want to connect all the actions to one action, use the code below in the same config file:
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Of course you should change the arguments to your own needs.
This configuration is located under app/config/routes.php.
To get more information about Route::connect(), visit this page: http://api.cakephp.org/class/router#method-Routerconnect
Yes, the default controller is PagesController, located in:
cake/libs/controller/pages_controller.php

Resources