URLManager, if Controller doesn't exist go to default controller

Hello everyone,

I’m fairly new to Yii and I’m running into the following issue within the URLManager.

I want to have Yii check if a controller exists, and if not, use the default one.

For example:

I create the pages about, contact and life. Those I want to be able to access through mysite/about, mysite/contact and mysite/life.

If I only had these kind of pages, I could use the rule:

‘<action:\w+>’ => ‘site/<action>’

Site being the defualt controller.

But if I then access, lets say, users, which has its own controller, it should use the controller Users when I access mysite/Users.

What is the way for this in Yii? Is there a ‘default’ option to realise such thing without adding all those paging as a rule (which slows down the application)?

Thanks.

This is exactly what I need to know, any updates on this?

Thank you in advance!

As for me, I just use .html for these urls (http://example.com/about.html)

and routing rule like


'<synonym:[\w\-]+>.html' => 'site/content'

Performance is important, but security is more important than performance. Dont worry about slowing down your application…

You should use something like this


'<controller:\w+>/<id:\w+>'=>'<controller>/view',

<controller:\w+>/<id:\w+> => <id:\w+> is username (mySite/Users)

you access "mySite\UserAsd" => mySite is a controller

<controller>/view => view is action which shows users information… etc

btw It works for me.

it works and you must access other controllers like this ‘yourController/index’…

and There is a one problem with




Yii::app()->createUrl('search');

$this->createUrl('search');



it generates ‘yourDomain.com/index.php/search’ which means ‘site/search’. But search is a controller…

Solved…




Yii::app()->createUrl('search/index');

$this->createUrl('search/index');



That’s not really what I want to achieve. What I want is this:

let’s say we have 2 controllers, cms, for the main cms content. And the controllers users.

If you go to www.example.com you will get the main controller, in this case cms, thats good. If you go to www.example.com/users/view you get the users controller. But, and here is the tricky part, lets say we have a contact page in the controller cms you would have to go to www.example.com/cms/contact. But thats not what I want. I wanna go to www.example.com/contact and the URLmanager should check if controller contact exists, if not, use the default controller, cms and view the contact page because contact wasn’t a controller so it’s a view. so the first string after the url could be a controller name or a view if controller doesn’t exists.

I hope you understand what I want to achieve here :roll eyes:

Ah, I see now what you did here. I tested this but it didn’t work. After I wrote the text above this one I changed some things in your code and it works fine now!:


'<action:[\w\-]+>.html' => 'site/<action>',

You could even delete the .html part, works too if you don’t want the .html in your url.




'rules'=>array(

    'contact'=>'cms/contact',

    'about'=>'cms/about',

    'user/<id:\w+>'=>'users/view',

),



simple

And what about dynamic pages? Let’s say, the website admin could make his own pages in his cms?


'rules'=>array(

    'contact'=>'cms/contact',

    'about'=>'cms/about',

    'user/<id:\w+>'=>'users/view',


    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

),

you can add these 2 rules.

domain.com/#contact - how does it look?


'rules'=>array(

    '#contact'=>'cms/contact',

    '#about'=>'cms/about',

    '#user/<id:\w+>'=>'users/view',


    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

),

thats a problem




'rules' => array(

'<view:\w+>' => 'cms/pages',

)



cms controller has CViewAction.

(index.php/cms/pages?view=contact) => ‘index.php/contact’

(index.php/cms/pages?view=about) => ‘index.php/about’

it could work

If you use:


'<action:[\w\-]+>' => 'site/<action>',

It works great for what I want to achieve. One downside, you can’t access the other controllers without defining the view in the url >:(

So, the original question is still unanswered, save for "workarounds".

Here are my rules:


	'<a:\w+>' => '/site/<a>',

        '<c:\w+>/' => '<c>',	

	'<c:\w+>/<id:\d+>'=>'<c>/view',

	'<c:\w+>/<a:\w+>/<id:\d+>'=>'<c>/<a>',

	'<c:\w+>/<a:\w+>'=>'<c>/<a>',

So, the "site" controller, my default, works fine. I can go to

mydomain.com/login , and it goes to "/site/login", just fine. Perfect so far.

Now, I have a "Users" controller, whose default action is Index.

When I go to

mydomain.com/users/ , I get the message that the system can’t find the requested action.

How can I make it so that I don’t have to go to


mydomain.com/users/index

?

I know there are workarounds, but I want to find out the "right way" to do this … Thanks much!