How to set the default controller in yii2 - php

I have UserControler and I run it in virtual server http://basic.com/index.php?r=user/index. How can I set up UserController and action index to be the default when I go to http://basic.com

Did you try in your config:
'defaultRoute' => 'user/index'
Default Controller

Like few people already said, you need to add defaultRoute in the configurations file.
Here is how it should look:
//config/web.php in basic template or backend/config/main.php in advanced
$config = [
...
'components' => [
...
],
'params' => $params,
'defaultRoute' => 'user/index',
];

This can be set within the config, see Default Controller:
[
'defaultRoute' => 'main',
]
But note that this is closely related to routing, which can be completely customized by urlManager component. Then if you want let's say domain/profile to behave like domain/user/profile then these rules for urlManager might be another way to go:
'rules' => array(
'<action:\w+>' => 'user/<action>', // <-- use UserController by default
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
Hopefully this will help someone :)

Open the web.php file from your configuration folder, and add the following:
'defaultRoute' => 'admin'
to your $config array.

Note that defaultRoute is ignored, when strictParsing is set to enabled in the urlManager config. Refer to the issue on GitHub: https://github.com/yiisoft/yii2/issues/5892
The following config is recommended as a measure:
[
...
'defaultRoute' => 'default/index',
...
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
...
'rules' => [
...
'' => '', // <- this line should be added
],
],
...
]

This is not the answer of question, however it might be helpful to know:
there is a catchAll property in yii\web\Application which is defined as:
The configuration specifying a controller action which should handle all user requests.
Usage:
'catchAll' => ['controller/action']
So, every request to http://basic.com will call controller/action

With yii2 I was able to do it in the urlmanager with:
'rules'=> [
['pattern'=>'<action>', 'route'=>'controller/<action>'],
]

Try the other solutions... If they do not work, then use my simple trick...
Just create an index.php page at the root.
Then, in that file, write this code:
return header('Location: http://your page location');

If 'defaultRoute' don't work - check settings for 'urlManager'. Maybe default route is set there.
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index', // this line should be chenged to ''=>''.
'<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
],
],

Related

Yii2 UrlManager config

I have a UrlRoute as below:
<?= Url::toRoute(['/site/show-post', 'id' => $model->id, 'type_id' => $model->type_id]) ?>
which give the result as
mysitename.com/site/show-post/1405?type_id=1
I want to add the post title to url and I modified the above as
<?= Url::toRoute(['/site/show-post', 'id' => $model->id, 'type_id' => $model->type_id,'title'=>$mdoel->title]) ?>
which results in:
mysitename.com/site/show-post/1405?type_id=1&title=your+post+title
So it is working fine. but I want to modify it like & it should work getting the same result.
mysitename.com/site/show-post/1405/type_id/1/your+post+title
even here 'id'=>$model->id is returning as I expect that is /show-post/1405 but the subsequent parameter is resulting in query-string
I also tried in config.php under - UrlManager like this:
'<controller:[A-Za-z-]+>/<id:\d+>/<type_id:\d+>/<title>' => '<controller>/<action>',
but it doesn't seems to work.
How I can achieve it like what I mentioned.
How about:
'<controller:[A-Za-z-]+>/<action:\w+>/<id:\d+>/<type_id:\d+>/<title>' => '<controller>/<action>',
The method you are following is correct and the results you are getting are the expected result. I am not sure what you meant by query language, but what understand is you want the clean urls instead of extra characters being added to your urls. For this you will have to implement pretty urls for yii2.
Here is how you can get pretty urls in yii2, add the following code to your configs:
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],

Cannot route to custom action in Yii2 controller

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'
]
],

How to override or edit code of vendor directory in yii2?

I need to do some changes in vendor/yiisoft/yii2/web/urlmanager.php for my url_alias to work! I need to know if I can change this file directly or is there any method to override this file?
Best way is to create new URL Manager Class that extends existing UrlManager class i.e. yii/web/urlManager.php
i.e
class customUrlManager extends yii/web/urlManager {
.. code here
}
Then specify the class in your config element i.e frontend/config/main.php
'urlManager' => [
'class' => '<name_space>/customUrlManager'
'enablePrettyUrl' => True,
'showScriptName' => False,
'rules' => [
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],

urlmanager with modules not working in yii2.0

This regarding module creation inside backend folder.Example i have created a module name as "api". And also sccessfully created controller for that modules.
here is my urlmanager code:
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'rules' => [
'<module:\w+>/<controller:\w+>/<action:\w+>'=>'<module>/<controller>/<action>',
'<module:\w+><controller:\w+>/<action:update|delete>/<id:\d+>' => '<module>/<controller>/<action>',
],
]
when i access url with respect to module as "api" , controller as "country" and action as "create"
http://local2host.com/bootstrap/backend/web/index.php/api/country/create
it showing 404 Not Found error
Where i am going wrong?
The second rule will never work, Also you do not have to define the rules and the normal ones will work. So this
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
will make this link /website.com/core/contact/index work just fine.
But I am not sure why your route is not catching the first rule... strange. It should.
Better late then never.
For make module works you need to append it in config:
'modules' => [
......
'modulename' => [
'class' => 'app\modules\modulename\Module',
],
......
],

Controller can't find the view in Yii

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);

Categories