Yii urlManager unlimited parameters - php

is there is a way in yii to make parameters unlimited
for example, i have module /admin/
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'admin/<controller:\w+>/<action:\w+>/<id:\d+>' => 'admin/<controller>/<action>',
'admin/<controller:\w+>/<action:\w+>'=>'admin/<controller>/<action>',
),
),
and within admin module i need that every action could have unlimited parameters, for example:
/admin/anycontroller/anyaction/anything
/admin/anycontroller/anyaction/anything/anything2
/admin/anycontroller/anyaction/anything/anything2/anything3
/admin/anycontroller/anyaction/anything/anything2/anything3/anything4
... and so on
should i define it one by one on the rules? or there is shortcut to do this?
and how to catch it on the controller action?

There is a shortcut:
'admin/<controller:\w+>/<action:\w+>/*'=>'admin/<controller>/<action>'
i.e append the rule with a /*
Since this is a more general rule, which can catch a lot of urls, it would be better to have it at the bottom, or atleast after any specific rules, i.e:
// ... other specific rules ...
'<controller:\w+>/<action:\w+>/<id:\w+>'=>'<controller>/<action>', // specifically for id
// ... other specific rules ...
'admin/<controller:\w+>/<action:\w+>/*'=>'admin/<controller>/<action>'
For your case:
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'admin/<controller:\w+>/<action:\w+>/<id:\d+>' => 'admin/<controller>/<action>',
'admin/<controller:\w+>/<action:\w+>'=>'admin/<controller>/<action>',
'admin/<controller:\w+>/<action:\w+>/*'=>'admin/<controller>/<action>'
To catch it in the action, just don't specify any parameters for the action, like so:
public function actionSomething() {
// instead use $_GET
$params=$_GET;
}
But it should also work with the definition that you already have: public function actionAnyAction($id=0,$type='',$type2='')

Related

Yii app()->createUrl for view action

I use generated actions by Gii in a module said News. I have normal view action that works with an id parameter (such as example.com/news/view/id/1).
When I use this line of code:
Yii::app()->createUrl("news/view",array("id"=>$data->primaryKey))
It generates example.com/news/1 (if $data->primaryKey is 1). It is not correct.
When I use this line of code:
Yii::app()->createUrl("news/view/id/",array("id"=>$data->primaryKey))
It generates example.com/news/id/id/1 (if $data->primaryKey is 1).
I am so confused! in first situation, this function doesn't generate id as a parameter name, and in second situation, it does! but after manually added id.
What shoud I do to make correct url format with this function?
Edit: news is a module. I changed the line of code as:
Yii::app()->createUrl("news/default/view/id/",array("id"=>$data->primaryKey))
It generates example.com/news/default/view/id/1 that is correct, but I don't want that default!
In config file you have something like this:
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
this create how to look the URL.
You not need to write the id parameter when you create URL because is default. Look on the urlmanager rules:
Yii::app()->createUrl("news/view/",array("id"=>$data->primaryKey)) => example.com/news/id/1
On module defaut:
Yii::app()->createUrl('/news/default/view', array('id' => $data->primaryKey))
You need to create the urlmanager rule... how you want to look at your URL. More details here.
Use
'rules'=>array(
'news/<controller:\w+>/<id:\d+>'=>'news/<controller>/view',
'news/<controller:\w+>/<action:\w+>/<id:\d+>'=>'news/<controller>/<action>',
'news/<controller:\w+>/<action:\w+>'=>'news/<controller>/<action>',
),
if you have a module names news.
You can try to replace news with regex but you will face problems with urls matched by several regexes if your regex is too broad. Use something like <module:news|accounting|people> in rules array key and <module> in rules array value.
If you need more sophisticated url management, or if you cannot solve your task with regexes, you can always extend CUrlManager.
trying to check it on this directory: protected/config/main.php sir.
urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controll<>er>/<action>',
),
),
Please notice is that routed well? :)

How to rename Yii's controllers using urlManager Component

I need to access my controllers with another name. For example, my controller name is *form_number_98*, but I need to access it by calling it through URL some thing like */form_98*.
All above mentioned will be done by the following line:
'form_<id:\d+>'=>'form_number_<id>',
But what if I need to access to it's view or another actions?
For example access to view with id 1
http://example.com/form_98/view/1
Which means:
http://example.com/form_number_98/view/id/1
Your answers would be greatly appreciated.
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
'/form_number_98/<action:\w+>/<id:\d+>' => 'form_98/<action>/'
),
),
You can achive it by using url manager. Imagine you have a controller called "SampleController" with an action called "actionSampless", then you can have something like this:
'urlManager'=>array(
'urlFormat'=>'path','showScriptName'=>false, 'caseSensitive'=>false,
'rules'=>array(
'test/test/*'=>'Sample/Sampless',
//'pattern1'=>'route1', (it goes like this)
),
),
for more info click here. read User-friendly URLs topic..
Hope it may help you.

URL rewriting with Yii framework

I have a url like
www.example.com/food/xyz
for which I have written a rule like this in the main.php (config folder)
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
'/food/<name:\w+>/' => 'food/index/',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
This means that internally "xyz" will be passed as a parameter to the actionIndex of my FoodController according to the rule
class FoodController extends Controller
{
public function actionIndex($name){
//some code here
}
public function actionItems(){
//some code here
}
}
The problem i'm facing is that I have another method in the same class called actionItems and when I use the URL
www.example.com/food/items
it calls the actionIndex and passes "items" as a parameter
Any idea on how to solve this?
Thanks in advance.
Your first rule should be for the items
'/food/items'=>'food/items',
'/food/<name:\w+>/' => 'food/index/',
Alternatively, I would rather use an alias to split the use of the controller such that all 'food'/<:name>' urls are differentiated from '/food/action' urls for example:
'/food/<name:\w+>/' => 'food/index/',
'/foods/<action:\w+>' => 'food/<action>'
Try this, just change order:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'/food/<name:\w+>/' => 'food/index/'
),
),

MongoDB Yii and the URL Manager

I'm having a bit of trouble with MongoID in and the url manager in Yii.
If I have a url like:
http://www.example.com/article/1
or
http://www.example.com/article/view/1
Everything works. Now for the problem. I am using MongoDB so my urls look like this:
http://www.example.com/article/50e18c655b0c4dce75000033
or
http://www.example.com/article/view/50e18c655b0c4dce75000033
And each scenario throws a 404 not found error, like it cannot go the actionView. My url manager in the config looks like this:
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>/<id:\d+>/<cid:\d+>'=>'<controller>/<action>',
),
),
What am I doing wrong with this and how do I get the MongoID to register as ids?
Had the same issue. Im my case it was invite system and it was very important to rule exactly hex, bcause I have InviteController as well.
'invite/<invite_key:[a-fA-F0-9-]+>'=>'site/invite',
in your case i suppose it should be something like this
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>/<id:[a-fA-F0-9-]+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>/<id:\d+>/<cid:\d+>'=>'<controller>/<action>',
),
),
Maybe you should change <id:[a-fA-F0-9-]+> to <cid:[a-fA-F0-9-]+>, I dont really know how your controller works.
I hope you found solution a bit earlier. But Google points here for the several request.

Hide all controllers from url

I've used Yii's urlManager to rewrite my url from mypage.com/controller/view to mypage.com/view. But I have more than one Controller and would like to apply this on all of them. Can this be done?
My current urlManager:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<action:\w+>' => 'site/<action>',
),
),
First way:
'<action:(login|logout|about)>' => 'site/<action>',
'<action:(view|edit)>' => 'product/<action>',
Second way to implement this feature is to use Using Custom URL Rule Classes

Categories