CakePHP: Hide the /index action using Routing - cakephp

What is the correct way to hide the /index action while having url parameters using Routing in CakePHP apps and the Html-Helper?
I want to be able to show urls like /books/2 while using the HTML helper
echo $this->Html->link('Books', array(
'controller'=>'books', 'action'=>'index', 2
));
Tried the following in my routes.php
Router::connect('/books/:id', array('controller' => 'books'), array(
'id' => '[0-9]+',
'pass' => array('id')
));
Output is: http://www.example.com/books/index/2
Wanted is: http://www.example.com/books/2

You'll need to use the id key in the routing array when creating links:
echo $this->Html->link('Books', array(
'controller'=>'books', 'action'=>'index', 'id' => 2
));

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.

want to remove action name from url CakePHP

i am working on a Cakephp 2.x.. i want to remove the action or controller name from url ... for example i am facing a problem is like that
i have a function name index on my Messages controller in which all the mobile numbers are displaying
the url is
www.myweb.com/Messages
now in my controller there is a second function whose name is messages in which i am getting the messages against the mobile number
so now my url becomes after clicking the number is
www.myweb.com/Messages/messages/823214
now i want to remove the action name messages because it looks weired...
want to have a url like this
www.myweb.com/Messages/823214
When connecting routes using Route elements you may want to have routed elements be passed arguments instead. By using the 3rd argument of Router::connect() you can define which route elements should also be made available as passed arguments:
// SomeController.php
public function messages($phoneNumber = null) {
// some code here...
}
// routes.php
Router::connect(
'/messages/:id', // E.g. /messages/number
array('controller' => 'messages', 'action' => 'messages'),
array(
// order matters since this will simply map ":id"
'id' => '[0-9]+'
)
);
and you can also refer link above given by me, hope it will work for you.
let me know if i can help you more.
REST Routing
The example in the question looks similar to REST routing, a built in feature which would map:
GET /recipes/123 RecipesController::view(123)
To enable rest routing just use Router::mapResources('controllername');
Individual route
If you want only to write a route for the one case in the question
it's necessary to use a star route:
Router::connect('/messages/*',
array(
'controller' => 'messages',
'action' => 'messages'
)
);
Usage:
echo Router::url(array(
'controller' => 'messages',
'action' => 'messages',
823214
));
// /messages/823214
This has drawbacks because it's not possible with this kind of route to validate what comes after /messages/. To avoid that requires using route parameters.
Router::connect('/messages/:id',
array(
'controller' => 'messages',
'action' => 'messages'
),
array(
'id' => '\d+',
)
);
Usage:
echo Router::url(array(
'controller' => 'messages',
'action' => 'messages',
'id' => 823214 // <- different usage
));
// /messages/823214
in config/routes.php
$routes->connect('/NAME-YOU-WANT/:id',
['controller' => 'CONTROLLER-NAME','action'=>'ACTIOn-NAME'])->setPass(['id'])->setPatterns(['id' => '[0-9]+']
);
You can use Cake-PHP's Routing Features. Check out this page.

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

CakePHP 2.0 Router::connect issue without visible id in url

I want following SEO url like:
www.example.com/users/profile/webfacer
I do not want to use the unique user to fetch from database.
I try to use the Router method connect in my AppController. but I realised that it isn't possible (or not knowing it right now to use it in their also used in routes.php does not helped) like this:
//in AppController
Router::connect('/users/profile/:name',
array(
'controller' => 'users',
'action' => 'profile'
) ,
array(
'pass' => array('id', 'name'),
'id' => '[0-9]+'
)
);
How can I reproduce this link (below the example) with this html link helper to send the id but not show it in the url:
$this->Html->link('webfacer',array(
'controller'=>'users',
'action'=>'profile',
'id'=>1,
'name'=>'webfacer'
));
This would output www.example.com/users/profile/username:webfacer that mean my router doesn't appear to my route options.
Has anybody had the same issues and solved this?
Because you haven't put the :id argument in your route string, Cake won't know what to do when you pass it in the helper, that is why it's just appending it as a normal param in the URL. There is no way to pass a "hidden" id with the URL, you're best bet is to either expose it or at the other end of the app write something that fetches the ID based on the username you pass (make sure this column is indexed and url-safe).
I would just simplify your route to this:
//in AppController
Router::connect('/users/profile/:name',
array(
'controller' => 'users',
'action' => 'profile'
) ,
array('pass' => array('name'),
)
);
And don't bother passing ID to the helper. In your profile action you'd just have something like this:
public function profile($name) {
$user = $this->User->find('first', array('conditions' => array('name' => $name)));
}

CakePHP: pagination and custom routes

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.

Resources