I have added this rule in url manager
'rules' => array(
'blog/<id:\d+>' => 'blog/index',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
)
and when I requested url mysite.com/blog/index/1 no error the action index of blog controller was called. But when I requested mysite.com/blog it displayed a error page not found.But I still want it to call action index of blog controller.
Blog/
Blog/Index
Blog/Index/1
All above requests will redirect you into the index page, because of its default behavior.
But Blog/1 will create a request for the action named "1".
Please try below configurations for your url manager, try setting 'urlSuffix' => '/' and let me know
'urlManager' => array(
'cacheID' => false,
'caseSensitive' => true,
'showScriptName' => false,
'urlFormat' => 'path',
'urlSuffix' => '/',
'useStrictParsing' => true,
'rules' => array(
'blog/<id:\d+>' => 'blog/index',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
)
)
Related
I'm new to Yii2 and their URL ruling is kinda tricky. I have a SiteController with an action like this
public function actionSuccessStories($slug = null)
{
// some codes
}
in my config i have this
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [
// Default routes
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
// page routes
'success-stories/<slug:\w+>' => 'site/success-stories',
// Remove 'site' parameter from URL
'<action:(.*)>' => 'site/<action>',
],
],
in my view i have this to generate my url
Url::to(['site/success-stories', 'slug' => 'slug_value'], true);
my problem is Url::to(); creates success-stories?slug=slug_value
instead of
success-stories/slug/slug_value am i doing this right? What i want to accomplish is the second format.
I've read this question related to mine but it covers only modules
Yii2 url manager don't parse urls with get parameter
Change your rules to:
'rules' => [
// page routes
'success-stories/<slug:\w+>' => 'site/success-stories',
// Default routes
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
// Remove 'site' parameter from URL
'<action:(.*)>' => 'site/<action>',
],
Order of rules is important, Yii2 will try to match rule one by one, if it fits - it will use it.
I'm new to the YII framework. I have to hide the default controller name and function name.
For example:
(Existing URL : http://localhost/food/store/home )
(Required URL : http://localhost/food/ )
In the YII framework config page, they have declared the urlManager as:
URL Manage in config page :
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>'=>'<controller>/index',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
'showScriptName'=>false,
)
Default Controller Declaration :
'defaultController'=>'store'
You can create specific rules without using placeholders like <controller> or <action>, you just need to make sure you add them before the rules for the general cases.
'urlManager' => [
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => [
//Add the rules for the specific cases
'' => 'store/home',
// The general case rules go after the specific cases
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>' => '<controller>/index',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
use alias will slove the problem
'urlManager' => array(
'urlFormat' => 'path',
'rules' => array(
'admin' => '/admin/default',
'mobile' => '/mobile/default', 'mobile/<alias:fees|aboutus|contactus|terms|policy|faq|aml|legal|news|testimonial>' => 'mobile/default/<alias>',
'<alias:fees|about|contactus|terms|privacypolicy|faq|aml|legal>' => 'site/<alias>',
'<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
'showScriptName' => false,
),
I'm learning on yii2,
I have a problem when make a url route.
here's my url configuration
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
// 'enableStrictParsing' => false,
'enablePrettyUrl' => true,
'rules' => array(
'category/<id:\S+>' => 'category/detail',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'request-password-reset' => 'site/request-password-reset',
'reset-password/<token:\S+>' => 'site/reset-password',
'profile/<id:[0-9a-zA-Z\-]+>/?' => 'profile/user',
'logout' => 'site/logout',
'login' => 'site/login',
),
],
The problem is in profile routing.
Here's the case:
http://localhost/myapps/profile/some-url/ -> work with added slash in
the end of url
http://localhost/myapps/profile/some-url -> without slash in end of url isn't working and error 404
http://localhost/myapps/profile/someurl -> not working error 404.
http://localhost/myapps/profile/first-second-third -> with our without slash in the end is working
I'm really confuse, I've been looking for previous question here and try out nothing working with my case.
Can somebody help with my problem?
Just move your Profile alias to the top of the list:
'profile/<id:[0-9a-zA-Z\-]+>/?' => 'profile/user',
'category/<id:\S+>' => 'category/detail',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'request-password-reset' => 'site/request-password-reset',
'reset-password/<token:\S+>' => 'site/reset-password',
'logout' => 'site/logout',
'login' => 'site/login',
As '<controller:\w+>/<action:\w+>' => '<controller>/<action>' was defined before 'profile/<id:[0-9a-zA-Z\-]+>/?' => 'profile/user', it will never reach profile, because it recognized it as a controller/action.
I have a url like this
http://localhost/yii2/category/my-custom-string-parameter
I need to get the text my-custom-string-parameter
And I setup the rules like this
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'category/<url:>' => 'category/view',
),
],
This always give me 404 error.
What should I do?
replace 'category/<url:>' => 'category/view',, with 'category/<id:\w+>' => 'category/view'
Routing to View need an id and to use a string use w+ not url:
Is it possible to have this kind of array:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'urlFormat' => 'path',
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'/admin' => '/admin/home',
'/admin/<controller:\w+>' => '/admin/<controller>',
'/admin/<controller:\w+>/<action:\w+>/<id:\d+>' => '/admin/<controller>/<action>',
'/admin/<controller:\w+>/<action:\w+>' => '/admin/<controller>/<action>',
),
),
Be read from a database table?
Why: I'm making a CMS in which I want to define a custom categories, pages and posts base so that I don't use a base controller.
Example: My Pages controller is PagesController and I can make a rule in URL manager like this:
/pages/some-page-alias
I want to be able to change /pages/, /categories/ and /posts/ to something localized, meaning to be able to change it i.e. on Bosnian:
/kategorije/ => /categories/,
/stranice/ => /pages/,
/clanci/ => /posts/,
Is there a solution for this, how it can be done?
Btw, I have not tried anything because I have no idea...
You can simply do this:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'urlFormat' => 'path',
'rules' => array(
'kategorije/<id:\d+>' => 'category/view',
'kategorije/<action:\w+>/<id:\d+>' => 'category/<action>',
'kategorije/<action:\w+>' => 'category/<action>',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'/admin' => '/admin/home',
'/admin/<controller:\w+>' => '/admin/<controller>',
'/admin/<controller:\w+>/<action:\w+>/<id:\d+>' => '/admin/<controller>/<action>',
'/admin/<controller:\w+>/<action:\w+>' => '/admin/<controller>/<action>',
),
),
URLManager prioritizes by what is listed first. So if you put in a custom rule above the standard rules it will run those rules first. Once it finds a rule that applies it breaks out.
As for me, your question is not clear.
If you want to store array in DB you can do it by serialize'ing it before.
If you want multiple names pointing to same controller you can use such a rule:
array(
'<_c:(stranica|page)>/<id:\d+>'=>'myController/view',
)
In this case if any of urls
http://example.com/stranica/123
http://example.com/page/123
is requested, it will point to
http://example.com/myController/view?_c=stranica&id=123
http://example.com/myController/view?_c=page&id=123
accordingly. See CUrlManager examples in API.