I'm listing users on my website with pagination of 15 users per page. It works just fine and I've re write the url like this.
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'caseSensitive' => false,
'rules' => array(
'people' => 'Users/index',
...
---
As you can see I re route users/index to people. When I create a url it just also works fine but this isn't working in dataprovider pagination as I'm using CListView to render users list. It creates url in the pagination like this: users/index/users_pagination/2 how to solve problem?
you may like this,
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'caseSensitive' => false,
'rules' => array(
'people/*' => 'users/index',
---
Related
I have catalogue with products. Urls for products looks like this domain.com/adv?id=14792.
I want to beautify urls like this domain.com/adv/14792.
In web.php I tried to like this
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [
'adv/<id:\d+>' => 'site/adv',
'<alias:[\w-]+>' => 'site/<alias>',
],
],
and there is no result.
I tried another variant in rules block:
'rules' => [
'adv/<id:\d+>' => 'adv',
'<alias:[\w-]+>' => 'site/<alias>',
],
and urls start to look like I want. But on this link I get 404 page.
Please use action with adv so that it work.
'rules' => [
'adv/<id:\d+>' => 'adv/<add some action here>',
'<alias:[\w-]+>' => 'site/<alias>',
],
I solved my problem by myself. This is my urlmanager module now:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'adv/<id:\d+>' => 'site/adv',
'<alias:[\w-]+>' => 'site/<alias>',
'<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
],
this construction build url from https://example.com/adv?id=123456 to https://example.com/adv/123456.
In view link build with
Html::a($insideEl,Url::to(['adv','id' => $ladvert->id]))
and in siteController action is
actionAdv($id)
Thanx.
I have following url for example:
http://mywebsite.com/testing/test
And I want it to be something like this depending on the current language:
http://mywebsite.com/en/testing/test
I have language stored in: Yii::$app->language
I tried to modify my URLManager (/config/web.php), but it doesn't work for me.
'urlManager' => [
'class' => 'yii\web\UrlManager',
'showScriptName' => false,
'enablePrettyUrl' => true,
'rules' => [
'/login' => 'site/login',
'/logout' => 'site/logout',
Yii::$app->language.'/'.'<controller:\w+>s'=>'<controller>/index',
Yii::$app->language.'/'.'<controller:\w+>/<id:\d+>/<title:\w+>'=>'<controller>/view',
Yii::$app->language.'/'.'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
Yii::$app->language.'/'.'<controller:\w+>/<action:\w+>/<id:\d+>/*'=>'<controller>/<action>',
],
],
Can You help me?
I found an extension. Even if you don't want to use it, you can look into it’s code to get an idea how it works. (Changing patterns / routing / etc.)
https://github.com/codemix/yii2-localeurls
I am building a RESTful API with Yii2 (advanced) and endpoints are working as expected apart from a custom one I need.
My urlManagerlooks like:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' =>
['class' => 'yii\rest\UrlRule', 'controller' => 'api/v1/step', 'pluralize' => false],
],
If I add a custom action into the StepController like so it will work just fine - Calling with http://example.com/api/v1/step/test
public function actionTest()
However if I want to pass an ID in through the path I will receive a 404 error - http://example.com/api/v1/step/test/1
public function actionTest($id)
Is there something I'm missing?
Edit: Adding notes that may help others.
My example above was simplified but what I wanted my URL to look like was like http://example.com/api/v1/step/test-by-foobar/1 with the called method to be public function actionTestByFoobar($id). However to get this to work you have to set the urlManager rule like the following which I didn't find obvious:
'api/v1/step/test-by-foobar/1' => 'api/v1/step/test-by-foobar',
Notice that the value is hyphenated, not in camel-case.
With your code you can pass an id like this :
http://example.com/api/v1/step/test?id=1
But if you want to do it like this:
http://example.com/api/v1/step/test/1
You should rewrite the url like below:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule',
'controller' => 'api/v1/step',
'pluralize' => false
],
/* You are missing this line below */
'api/v1/step/test/<id:\d+>' => 'api/v1/step/test'
]
],
I want a view from another module to be rendered and I use path '//modulename/foldername/viewname'(without .php), but when I run it I get "Controller can't find the view" error. What do I do wrong? It used to work before. My version of Yii is 1.1.15.
EDIT:
These is my urlManager:
'urlManager' => array(
'class' => 'yupe\components\urlManager\LangUrlManager',
'languageInPath' => true,
'langParam' => 'language',
'urlFormat' => 'path',
'showScriptName' => false,
'cacheID' => 'cache',
'rules' => array(
)
But how can it be related to my problem? Doesn't urlManager has nothing to do with view aliases?
give it the full path to that file:
$this->render('application.modules.modulename.foldername.viewname' , $arrayData);
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.