Yii UrlManager route to own controller instead of module - php

I have just gotten started with the Yii framework, trying to create a simple application. I have added the yii-user-management module (YUM) and have followed the short installation tutorial.
Following the tutorial, I created a RegistrationController that extends the YumRegistrationController - then tried loading it in the browser. However, regardless of how I change the config for UrlManager, I cannot get my own controller or view loaded (the view was copied to protected/views/registration.registration.php as instructed).
I am not the only one with the same problem - see here for example - but I cannot get any of the solutions working. Not even renaming the controller has worked, so I figure that there is something fundamentally wrong with my UrlManager settings.
'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>',
),
),
The YumRegistrationController gets loaded every time, or I get a 404 error.
I have tried additional rules, like:
'registration' => 'application.controllers.Registration',
or (after renaming)
'registration' => '//MyRegistration/registration',
and even
'registration' => '//registration/registration/registration',
like in an article I read, but nothing worked.
Please help - I would really love to move on to creating an app instead of just working on the setup...
UPDATE: /registration/registration/registration works, it just shows the default view and loads the controller for YUM registration...
Yii version 1.1.10

I think , you have user component registered in your config main.php. So please share that. I can help.
. 'loginUrl' => array('/user/user/login'), may cause problem. no need to change urlManager.
'user'=>array(
'class'=>'application.components.WebUser',
'allowAutoLogin'=>true,
'loginUrl' => array('/user/login'),
),

Related

Yii 1 Pagination showing different behaviour in different controllers

In my Yii application, some controllers returning pagination URL as follows:
http://example.com/blog/index?page=2
And if I have written the same code in some other controller, then the pagination URL shows as follows:
http://example.com/blog/index/page/2
Any idea why it showing differently in different controllers? I need the page number as a query string (like the first URL).
Finally I got the issue, I have missed the following rule in my route file, which caused the issue.
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
This should be added as follows:
return array(
......
'components'=>array(
......
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
),
);

Yii: How do I find out which views, controllers, models and styles are being used in a page?

I'm very new to Yii, and I've been asked to take a look at a project, and see if I can add anything.
So to start off, I wanted to find out what files are in play for a certain page, i.e., what Controller, what View, what Model, etc. A friend, who dabbles in Yii told me it can usually be found via the URL itself, like this:
Sample: localhost/project/index.php?r=site/index
Site is the Controller, index is the Action
However, the project I saw returns URLs like so:
localhost/cdforum/web/index.php/forum/view/id/1
To which my friend said "the htaccess must've been modified". We assumed the Controller is forum, and the Action is view
We're not exactly sure if that's accurate. And given a project directory like so:
I'm not exactly sure what to look for. So I'd like to ask, for the URL above, is there a way to tell which files are responsible for the output?
You can usually get that from url but not necessarily because routing is defined in your config/main.php file in this part of it:
array(
......
'components'=>array(
......
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'pattern1'=>'route1',
'pattern2'=>'route2',
'pattern3'=>'route3',
),
),
),
);
Check the rules key of this array is the pattern the url is going to have so your pattern will look like
'forum/view/...' => 'the/real/url'
then stuff before first backslash is the controller and the second is the action. In that action you will be able to find what models are used.
Hope it helps
I suggest you to familiarize with this wiki page
http://www.yiiframework.com/wiki/249/understanding-the-view-rendering-flow/
Then, you can display trace log on your pages:
config/main.php
'log'=>array(
(...)
'routes'=>array(
(...)
array(
'class'=>'CWebLogRoute',
'levels'=>'trace',
),
),
),

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.

How can I redirect to another entry script?

I have two entry scripts on my application, as specified here. How can I set the route to redirect /example.com/admin to example.com/backend.php
Thanks.
Take a look at the documentation about URL management:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url
Without knowing the details of your setup, in your protected/config/main.php (if main.php is you application configuration file), you would need something along this:
// note: this is extracted from a project which uses 'friedly urls',
// depending on your setup, YMMV
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'/admin'=>'/backend', // <-- define your custom routes/redirects here
),
),

Categories