Pretty urls not redirecting in Yii2 - yii

I have an action name cart in site controller and I have used pretty URL so my URL is
WWW.test.com/cart
But I want to redirect if someone enter WWW.test.com/cart/,
then it should be redirected to WWW.test.com/cart.
I don't have cart controller.

You need to use UrlNormalizer for that:
'urlManager' => [
// ...
'normalizer' => [
'class' => yii\web\UrlNormalizer::class,
],
],
See documentation about URL normalization for more info.

Related

Two Yii2 applications Login conflict

I have two different Yii2 Basic template applications on the same hosting. When I login to the first Yii2 app and then go to login the secon Yii2 app, the first automatically logs out and vice versa. They use different cookieValidationKey in config. How to fix this.
I'm not sure if it fixes your problem but try to change configuration for identityCookie in one of the applications.
'components' => [
// ...
'user' => [
// ...
'identityCookie' => [
'name' => '_identity', // <-- change _identity to something else
'httpOnly' => true
]
]
]

Yii2 remove site/index from url

I use the Yii2 framework and want to remove site/index from my URLs.
Right now I have the following URL
example.com/site/index
but would like to change it to simple
example.com
I tried the following approach Yii2 How to remove site/index and page parameter from url but it doesn't work.
Could you please suggest how it can be done?
Looking at the question also asked at Yii2: Remove controller from URL
I ran into problems and I had to use the Yii2 resources as https://yii2-cookbook.readthedocs.io/enable-pretty-urls/
and this is what I got
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<action>'=>'site/<action>',
'<controller:[\w\-]+>/<id:\d+>' => '<controller>/view',
'<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>' => '<controller>/<action>',
'<controller:[\w\-]+>/<action:[\w\-]+' => '<controller>/<action>',
],
],
You need to add rule for empty path at the beginning of your rules:
'urlManager' => [
'rules' => [
'' => 'site/index',
// rest of rules
],
],

How to specify new subdirectory in routes for Yii 2 module?

I'm using Yii 2 and building a RESTful API inside a Yii 2 module called apiv1.
The file config.php for the module apiv1 looks like this:
// ...
urlManager' => [
// ...
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => [
'likes',
],
],
],
];
For instance, GET /apiv1/likes works, but I'd like to set up a route to handle GET /api/v1/likes. How can this be done either individually or for the entire module as a general route from api/v1 to apiv1?
You can use the prefix attribute to customize your rest/UrlRule routes.
E.g., for your case, you should be able to do:
[
'class' => 'yii\rest\UrlRule',
'controller' => 'likes',
'prefix' => 'api/v1',
]
For more info, you can see the REST routing guide and yii-rest-rule API docs - in particular, see the $patterns and $extraPatterns properties for additional configuration options.

Routing in module doesn't work Yii 2

I am new in Yii 2 and my problem is about routing inside a module.
I have a module in my app which is a profile cabinet both for users and admins. I created a CabinetController instead of DefaultController and also I created a AdminController and UserController.
What I want? I want this CabinetController received request and forward it to either AdminController or UserController after verify wether the user is admin or not.
In config file I set a default route for module as "cabinet"(as I understand this is a name for default controller). And in "rules" part of UrlManager I wrote following:
'modules' => [
'cabinet' => [
'class' => 'app\modules\cabinet\Module',
'defaultRoute' => 'cabinet'
],
'utility' => [
'class' => 'c006\utility\migration\Module',
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<_c:\w+>/' => '<_c>/',
'<_c:[\w\-]+>/<_a:\w+>' => '<_c>/<_a>',
'<_m:cabinet>/<_a:\w+>' => '<_a>',
],
],
If I go to "my-site.com/cabinet" it works fine and open "admin/index" because I made it to redirest this request to AdminController/actionIndex, but once I go to somewhere like "my-site.com/cabinet/users" it respond with 404 NotFound. I open the loger and see: exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "cabinet/desktop"
This is my CabinetController and the way I forward requests to Admin/UserController:
public function init()
{
parent::init();
$this->user = $this->findModel();
$this->controllerToUse = $this->user->isAdmin() ? 'admin' : 'user';
}
public function actionIndex()
{
return $this->module->runAction($this->controllerToUse . '/' . $this->action->id);
}
If I change defaultAction in CabinetController it run this action normally as expected. Or if I go to "my-site.com/cabinet/admin/users" again it works good, because it found a controller in the url(as I think).
Routing can be a bit tricky in Yii2, it follows a few rules you need to understand which can be found here
But if i understand you correctly Admin/UserController is part of the Cabinet module? and you want Yii to route /cabinet/users to /cabinet/admin/users
You'll need to add some rules in your UrlManager see Rules
Example:
'rules' => [
'<module:cabinet>/<action:\w+>' => '<module>/admin/<action>',
],
Hope it helps

Yii2 subdomain URL routing

I have two instances. The first one is actually my WordPress page and the second one is for the app. I've created an A record that points to the IP address where the instance for the app is. Let's say it's example.com
When I open app.example.com everything looks fine except links.
This is how I format the link:
<a href="<?php echo Url::to(['site/xyz/']);>xyzy/a>
However, my links are not formatted very well.
Do you know what can be a problem?
I've found this post: Yii2 - subdomain routing but I couldn't find it useful.
Maybe I need to add Virtual Host or..?
UPDATE:
My UrlManager rules:
'rules' => [
'app.example.com/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'' => 'site',
'logout' => 'site/logout',
'sign-up' => 'site/signup',
]
Also, the links are is as the following:
<a class=" "href="//import/">anchor</a>
Thanks everyone
Try to add some more rules like this:
'rules' => [
'app.example.com/<controller:\w+>/<id:\d+>' => '<controller>/view',
'app.example.com/<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'app.example.com/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
//'app.example.com/<controller:\w+>' => '<controller>/index',
'app.example.com/<action:\w+>' => 'site/<action>',
],