Cakephp routing with optional params - php

I have a method in my users controller similar to:
function members($type = null, $category = null) { ... }
Both params are optional and can be used together or on their own.
So with the following route.
Router::connect('/members/*', array('controller' => 'users', 'action' => 'members'));
http://example.com/users/members successfully becomes http://example.com/members.
Unfortunately the following don't work
http://example.com/members/type:cat
http://example.com/members/category:dog
http://example.com/members/type:cat/category:dog
how could I set up my routes so that they all work properly?

Named parameters aren't automagicaly mapped to the action. You can either get them by calling
$this->passedArgs['type'] or $this->passedArgs['category']
or by using the 3rd parameter in the Router::connect:
Router::connect(
'/members/*',
array('controller' => 'users', 'action' => 'members'),
array(
'pass' => array('type', 'category')
)
);
http://book.cakephp.org/view/46/Routes-Configuration

Try with
Router::connect('/members/type\:(.*)', array('controller' => 'users', 'action' => 'members_type'));
Router::connect('/members/category\:(.*)', array('controller' => 'users', 'action' => 'members_category'));
Router::connect('/members/type\:(.*)/category:(.*)', array('controller' => 'users', 'action' => 'members_type'));
Note that I didn't test it, but I think you must escape the colon.

Related

Kohana more than one dynamic routing for specific urls

I don't know if someone else is using kohana (koseven with new name) framework for develepment. I need help about routing. I am migrating an asp site to php with using koseven ( kohana) framework and I must keep all the url routing on current site. Because of this I must use more than one routing on my project.
Url structer must be like this:
domain.com/contenttype/contentid -> contenttype is dynamic and gets data over Content Controller
domain.com/profile/username ->profile is the controller and index is the action. I must get the user name from id parameter.
domain.com/categories/categorname (Works fine-> categories is the controller, index is the action and categorname is the id parameter.
There is an admin page on my site and using a directory route on it.
Here is my route on bootstrap.php file:
Route::set('panel', '<directory>(/<controller>(/<action>(/<id>)))', array('directory' => 'panel'))
->defaults(array(
'controller' => 'panel',
'action' => 'index',
));
Route::set('kategori','<kategori>(/<id>)', array('id'=>'.*'))
->defaults([
'controller'=>'kategori',
'action'=>'index',
]);
Route::set('default', '(<controller>(/<action>(/<id>)))', array('id'=>'.*'))
->defaults([
'controller' => 'anasayfa',
'action' => 'index',
]);
First Problem: If I copy kategori route for profile it uses kategori route instead of profile.
Second Problem: How can I get dynamic routing for the contenttype. Content controller is the default controller and it will list the contents under the dynamic contenttype if there isn't given any content title on the id parameter. If the id parameter is identified at this time it will Show the detail of content.
Thanks.
Route::set('panel', 'panel(/<controller>(/<action>(/<id>)))', ['controller' => '\w+', 'action' => '\w+', 'id' => '[-\w]+'])
->defaults(array(
'directory' => 'panel',
'controller' => 'dashboard',
'action' => 'index',
'id' => null,
));
Route::set('kategori','<kategori>(/<id>)', ['kategori' => '[-\w]+', 'id' => '[-\w]+'])
->defaults([
'controller' => 'kategori',
'action' => 'index',
'id' => null,
])
->filter(function ($route, $params, $request) {
$model = ORM::factory('Kategori', ['kategori' => $params['kategori'], 'id' => $params['id']]);
if ($model->loaded()) {
$params['model'] = $model;
return $params;
}
return false;
});
Route::set('default', '(<controller>(/<action>(/<id>)))', ['controller' => '\w+', 'action' => '\w+', 'id' => '[-\w]+'])
->defaults([
'controller' => 'anasayfa',
'action' => 'index',
'id' => null,
]);

CakePHP Routing - URL With Parameters Only

I have two function "product" and "view" in cakephp. If any one type domainname.com/item1 then call "product" function and if domainname.com/item1/item2 then call "view" function .
item1 and item2 is dynamic content.
Router::connect('/:category', array('controller' => 'Posts', 'action' => 'product'));
Router::connect('/:category/:title', array('controller' => 'Posts', 'action' => 'view'));
I use this code in routes.php
Problem is this if I enter domainname.com/item1 then it is call view function.
please suggest me how to use url rewriting in cakephp .
try to add /posts and specifie the order like this:
Router::connect('/posts/:category/:title',
array('controller' => 'Posts', 'action' => 'view'),
array(
// order matters since this will simply map ":category" to $category in your action
'pass' => array('category', 'title')
)
);
You can take a look to the doc http://book.cakephp.org/2.0/fr/development/routing.html
try this
Router::connect('/:category/:product',
array('controller' => 'posts', 'action' => 'view'),
array('pass' =>
array('product')
));
Router::connect('/:product', array('controller' => 'posts', 'action' => 'product'));

Can i hide controller and view name cake php 2?

I am using cake php and due to some reason i want to hide controller and action name from the url . current url us like
http://192.168.1.31/home/this_is_test
where home is controller name and this_is_test is slug which is dynamic . i want the url like
http://192.168.1.31/this_is_test.
my routes.php is
Router::connect('/', array('controller' => 'home', 'action' => 'index'));
Router::connect('/dashboard', array('controller' => 'dashboard', 'action' => 'index'));
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/admin/login', array('controller' => 'users', 'action' => 'login', 'admin' => true));
Router::connect('/contents/*', array('controller' => 'contents', 'action' => 'view'));
Router::connect('/home/*', array('controller' => 'Home', 'action' => 'index'));
I have read a couple of solution after googling . also tried this in routes.php . but no luck
Router::connect(
'/:query',
array('controller' => 'Home', 'action' => 'index',1),
array('query' => '[a-zA-Z]+')
);
anybody have idea about this if it is possible??
Your solution
For static text try this:
Router::connect('/this_is_test', array(
'controller' => 'home',
'action' => 'this_is_test OR any_other action name'
));
If it's dynamic
Router::connect('/:id',
array('controller' => 'home', 'action' => 'index'),
array(
'pass' => array('id'),
array('id' => '[A-Za-z]')
)
);
References: Cakephp2.x Route
I hope I knew what you really want to achieve. You can place the Route in the last position. Here is the reference .
Other option would be to use alias for your controller. So you call your controller some thing else and set a new name for your controller then call it in you Route.
If this doesn't work then you would need to write a bespoke Component in order to help you to do that.

Different view route in CakePHP

I can somehow change the generated and accepted routes for simple urls, in the routes.php:
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/register', array('controller' => 'users', 'action' => 'add'));
This works like a charm. However, this doesn't:
Router::connect('/eintrag/:id', array('controller' => 'entries', 'action' => 'view'));
Router::connect('/bearbeiten/:id', array('controller' => 'entries', 'action' => 'edit'));
When I try to get a route for this, via echo $this->Html->url(array('controller' => 'entries', 'action' => 'view', $entry['id'])), I get /entries/view/1. And the url /eintrag/1 is not accepted by the router.
How can I prettify my view and edit routes like I can do with parameterless routes?
You need to use a third param in your route, as you are passing it :id specifically.
// SomeController.php
public function view($id = null) {
// some code here...
}
// routes.php
Router::connect(
'/eintrag/:id', // e.g. /eintrag/1
array('controller' => 'entries', 'action' => 'view'),
array(
// this will map ":id" to $id in your action
'pass' => array('id'),
'id' => '[0-9]+'
)
);
should do it.
More info # the cookbook
$this->Html->url() is just a Helper Function, that simply generates a URL according to paramaters passed, however when you actually open this Url then it will route request made for /eintrag/1 to /entries/view/1

CakePHP route with regex

I have a controller setup to accept two vars: /clients/view/var1/var2
And I want to show it as /var1/var2
SO i tried
Router::connect('/*', array('admin'=>false, 'controller' => 'clients', 'action' => 'view'));
But this stops all other controllers working as /* routes everything
All other pages that are on the site are within the admin prefix so basically i need a route that is ignored if the current prefix is admin! I tried this (regex is from Regular expression to match a line that doesn't contain a word?):
Router::connect('/:one', array('admin'=>false, 'controller' => 'clients', 'action' => 'view'), array(
'one' => '^((?!admin).*)$'
));
But I think the regex is incorrect because if i naviate to /test it asks for the tests controller, not clients
My only other routes are:
Router::connect('/admin', array('admin'=>true, 'controller' => 'clients', 'action' => 'index'));
Router::connect('/', array('admin'=>false, 'controller' => 'users', 'action' => 'login'));
What am I doing wrong? Thanks.
I misunderstood your question the first time. I tested your code and didn't get the expected result either. The reason might be that the regex parser doesn't support negative lookahead assertion. But I still think you can solve this with reordering the routes:
The CakeBook describes which routes are automatically generated if you use prefix routing. In your case these routes have to be assigned manually before the '/*'-route to catch all admin actions. Here is the code that worked for me:
// the previously defined routes
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/admin', array('controller' => 'clients', 'action' => 'index', 'admin' => true));
// prefix routing default routes with admin prefix
Router::connect("/admin/:controller", array('action' => 'index', 'prefix' => 'admin', 'admin' => true));
Router::connect("/admin/:controller/:action/*", array('prefix' => 'admin', 'admin' => true));
// the 'handle all the rest' route, without regex
Router::connect(
'/*',
array('admin'=>false, 'controller' => 'clients', 'action' => 'view'),
array()
);
Now I get all my admin controller actions with the admin prefix and /test1/test2 gets redirected to the client controller.
I think the solution is described in the bakery article on routing - "Passing parameters to the action" (code not tested):
Router::connect(
'/clients/view/:var1/:var2/*',
array(
'controller' => 'clients',
'action' => 'view'
),
array(
'pass' => array(
'var1',
'var2'
)
)
);
The controller action would look like:
public function view($var1 = null, $var2 = null) {
// do controller stuff
}
Also you have too look at the order of your routes (read section "The order of the routes matters"). In your example the '/*' stops all other routes if it comes first, if you assign the rule after the others it handles only requests which didn't match any other route.

Categories