Routing in module doesn't work Yii 2 - yii

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

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

How to change yii2 default module

Hello I am not an expert in Yii2 and would appreciate any help, We want to change our default module,
Our logic:
site implements a use of wildcard domain, https://example.com,
we implement a bootstrap component to Identify a use of a "subdomain" in the
url I.E. https://sub.example.com,
$config = [
'id' => 'basic',
'name' => 'exapmle',
'basePath' => dirname(__DIR__),
'bootstrap' => [
'log',
'devlogin',
'app\components\SubBootstrap', #this is the bootstrap component we use
'app\components\ThemeBootstrap',
],...
now we would have liked to use the same logic to change the default module to a new "submodule" but we can't use the bootstrap because it happens after the default module has been applied.
obviously we can have an explicit url call for the module I.E.
'modules' => [
'sub'=>[
'class' => 'app\modules\sub\Module',
],...
but that means that the url would look like https://somesub.example.com/sub/ which is undesirable.
thank you very much
In your case, what you can do is override the UrlManager component and manually adjust the path to reflect the module that you want to envoke behind the scenes.
So your code would look something like this:
<?php
namespace app\components;
use Yii;
class UrlManager extends \yii\web\UrlManager
{
public function parseRequest($request)
{
if (!empty(Yii::$app->sub)) {
$pathInfo = $request->pathInfo;
$moduleIds = array_keys(Yii::$app->modules);
$inModule = false;
foreach ($moduleIds as $moduleId) {
if (preg_match("/^{$moduleId}/", $pathInfo)) {
$inModule = true;
break;
}
}
if (!$inModule) {
$pathInfo = 'sub/' . $pathInfo;
$request->setPathInfo($pathInfo);
}
}
return parent::parseRequest($request);
}
}
and then in config/web.php:
'urlManager' => [
'class' => 'app\components\UrlManager',
...
],
You don't need to change the module configuration. You need to change web-server path to this module and architech the UrlManager rules;
https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing
Bootstraping yii module it's only some way to load it before another components.
https://www.yiiframework.com/doc/guide/2.0/en/runtime-bootstrapping

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.

Can't make the UrlManager Rules work in Yii2

I understand many variables outside what I can provide could be the problem, but I'm still asking if someone had this problem and could help.
Here's my UrlManager config in the components
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => false,
'showScriptName' => true,
'rules' => [
'' => 'site/index',
'member' => 'site/login',
],
],
This url works:
http://exampler.com/web/index.php?r=site/login
This url returns a 404
http://example.com/web/index.php?r=member
**NOTE: ** I don't have any messy Nginx or Apache rules on my server like this guy had. My rules seem to be completely ignored, whatever I write in them.
$rules is ignored if you set $enablePrettyUrl to false. From $rules documentation:
The rules for creating and parsing URLs when $enablePrettyUrl is true. This property is used only if $enablePrettyUrl is true.
https://www.yiiframework.com/doc/api/2.0/yii-web-urlmanager#$rules-detail

Beautifying URLs in Yii

I have following file structure
As default the url created for accessing the module content is for example
http://127.0.0.1/tmc/user/default/viewMessage
and for other controller it comes out to be
http://127.0.0.1/tmc/user/booking/index
The problem is I want to write a rule in my urlManager so that both controllers remain accessible AND i do not see default word in url as in first example.
However if i write following rules I am able to eliminate the default word but now other controllers in same module wont work. any help in this regard is appreciated
'<module:\w+>/<action:\w+>/<id:(.*?)>' => '<module>/default/<action>/<id>',
'<module:\w+>/<action:\w+>' => '<module>/default/<action>',
My current Url Manager is as follow
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => false,
'rules' => array(
'/' => 'site/index',
'login' => 'site/login',
'user' => 'user/default/',
'<view:[a-zA-Z0-9-]+>/' => 'site/page',
),
),
Follow this Link for config settings
Refer this Link
//inside protected/modules/admin/AdminModule.php
class AdminModule extends CWebModule
{
//goes to TaskController instead of DefaultController
public $defaultController = 'Task';
...
Now your Yii application will routes to the “TaskController” if you request
index.php?r=admin
//same as requesting
index.php?r=admin/task