I've problem with url in Yii.
when I enter URL like this,
http://localhost/yii_project/index.php/gii/default/login
it doesn't opens that page and it redirects to home page.
But when I enter url like this,
http://localhost/yii_project/index.php?r=gii/default/login
it open successfully.
I don't want to use index.php?r=. I want to display it index.php/gii/..
Change /protected/config/main.php as
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'search/<action:\w+>/'=>'dashboard/search/<action>',
'view/<action:\w+>/'=>'dashboard/view/<action>',
),
for more Info Hide index.php in yii
This is Yii1 or Yii2 you are using ?
For Yii2 Basic App (current version), open #app/config/web.php and add the following code:
This will result in this:
http://localhost/yii_project/gii/default/login
If you wanted the index.php included also then change a line to 'showScriptName' => true,
return [
... ,
'components' => [
.... ,
// below is the new section to include:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => false,
//'suffix' => '.php',
//'cache' => 'cache',
//'scriptUrl' => '',
//'baseUrl' => '/',
//'hostInfo' => 'http://www.yourhost.com.au',
//'routeParam' => 'r',
'ruleConfig' => [
'class' => 'yii\web\UrlRule'
],
'rules' => array(
[
'pattern' => 'gii',
'route' => 'gii',
'suffix' => '',
],
[
'pattern' => '/<controller:\w+>/<action:\w+>',
'route' => 'gii/<controller>/<action>',
'suffix' => '',
],
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
... ,
],
];
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 am using nginx and yii2 trying to create a little sample crud rest API, and whenever I enable pretty urls I can't access my routes anmymore.
My config is as follows :
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'user',
'only' => ['delete', 'create', 'update', 'get'],
'patterns' => [
'PUT users/<id:\d+>' => 'user/update',
'DELETE users/<id:\d+>' => 'user/delete',
'GET users/<id:\d+>' => 'user/get',
'POST users' => 'user/create',
]
],
[
'class' => 'yii\rest\UrlRule',
'controller' => 'campaign',
'only' => ['delete', 'create', 'update', 'get'],
'patterns' => [
'PUT campaigns/<id:\d+>' => 'campaign/update',
'DELETE campaigns/<id:\d+>' => 'campaign/delete',
'GET campaigns/<id:\d+>' => 'campaign/get',
'POST campaigns' => 'campaign/create',
]
],
],
],
Note that when enabling this, I can access gii without any issue at /gii instead of /index.php?r=gii
When disabled, my routes work fine using index parameters. Also it's yii giving me 404 and not directly nginx.
EDIT: This url works /user/get?id=1
However this one does not /users/get/1, nor /user/get/1
A POST on /users will work, but not any of the other http requests
I have a problem calling the url of my rest api in Yii2. I want to call a url like:
http://localhost/index-dev.php/myapi/collection/18
where 18 is the Id.
In my web.php config, I have the following code, with other settings from another programmers :
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => true,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => ['coding/nodes', 'coding/scales','myapi/collection']],
'<controller:\w+>/<id:\d+>' => '<controller>/view',
],
],
when i call my URL, i get
Not Found (#404)
What am i doing wrong?
I had the same problem, you can disable the prural
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => true,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => ['coding/nodes', 'coding/scales','myapi/collection']],
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'pluralize' => false,
],
],
You need to use the plural of your model class name in the URL for accessing a single model:
http://localhost/index-dev.php/myapi/collections/18
Take a look at the documentation of yii\rest\UrlRule:
The above code will create a whole set of URL rules supporting the following RESTful API endpoints:
'PUT,PATCH users/<id>' => 'user/update': update a user
'DELETE users/<id>' => 'user/delete': delete a user
'GET,HEAD users/<id>' => 'user/view': return the details/overview/options of a user
'POST users' => 'user/create': create a new user
'GET,HEAD users' => 'user/index': return a list/overview/options of users
'users/<id>' => 'user/options': process all unhandled verbs of a user
'users' => 'user/options': process all unhandled verbs of user collection
i suggest create separate module for API
and configure your UrlManager like...:)
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'pluralize'=>false,
'controller' => ['v1/country','v1/user'],
'tokens' => [
'{id}' => '<id:\\w+>'
],
'extraPatterns' => [
'POST register' => 'register', //from url
'GET exists'=>'exists',
'POST login'=>'login',
'POST follow'=>'follow',
'POST category'=>'category',
'PUT profile'=>'profile',
'PUT change_password'=>'change_password',
'PUT feed_interested'=>'feed_interested',
],
]
],
]
More detail # Create Rest Api
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:
I'm using Yii2 and have been using the Url::to method without any issue. Now I just tried it with params for the first time and it strips the ending slash from my route.
So normally, say I would do this:
Url::to('/my/route/', true);
// http://www.example.com/my/route/
But if I want to pass params, such as:
Url::to('['/my/route/', 'id' => 123, 'name' => 'larry']', true);
// http://www.example.com/my/route?id=123&name=larry
Is there any way to stop it doing this, if not, do I have any other options to get what I want?
My UrlManager rule:
'rules' => [
[
'pattern' => 'foo/<name:[0-9a-zA-Z\-]+>.<some_id:\d+>',
'route' => 'foo/index',
'suffix' => '/',
],
//.........
],
1) You can achieve that by adding suffix to corresponding url rule containing that route:
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
[
'pattern' => 'my/route',
'route' => 'my/route',
'suffix' => '/',
],
],
],
],
2) Globally it's available through urlManager property with the same name suffix.
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'suffix' => '/',
],
],
3) Alternative way of setting through component:
Yii::$app->urlManager->suffix = '/';
This one works for specific routes too, just add this line before calling Url::to();