LangSwitcher problem with module urlRules

Hello, i got problem with LangSwitcher, when i use urlRules for module LangSwitcher redirects to homepage.

class Module extends \luya\base\Module
{
    public $urlRules = [
        [
            'pattern' => 'projects/<id:\d+>',
            'route' => 'projects/default/detail',
            'composition' => [
                'pl' => 'projekty/<id:\d+>',
                'en' => 'projects/<id:\d+>',
            ],
        ],
    ];
}

When i create cms page with this module, its not working with params (?id=x), so in LangSwitcher#L243 i had to generate our url with id params etc. When i want to use urlManager with createUrl its working bad too because it adds /en/ before the link

Hi @tomekat

What is the name of your Module? Because when creating url rules, the CMS needs to know where to replace the current page slug, therefore its required to prefix the pattern and route.

If you are using the module in a CMS context your patterns must be prefix with the module name, otherwise the CMS can not auto replace the new pattern with the CMS context information. For example redirecting from controller foo action index to controller bar action index inside a module both url rules must be prefix. For foo [‘MODULE/name-for-foo-index’ => ‘MODULE/foo/index’] for bar [‘MODULE/name-for-bar-index’ => ‘MODULE/bar/index’] so they must have the full qualified route with the name of the module used in the config to register the module. Also the patterns must prefix the module name.

But i think you made that correct, assuming the module name (also in the config!) is projects. So your url is created correctly, but not when switch to another language with the LangSwitcher widget? Is that correct?

Maybe take a look at this: https://luya.io/api/luya-cms-widgets-LangSwitcher#setUrlRuleParam()-detail

In order to create an url inside the projects/default/index controller you can use Url::toRoute(['detail', 'id' => 1]);

detail function got id param

well now i got “project?id=x” from cms page and langswitcher generate it without params “id …”

Our cms page dont have urlRules. Where i can set it? in config urlManager? i trying but it isnt work. i want to generate /project/id url.

Thanks for help nadar :slight_smile:

Okay that is a very special case, you are defining the detail route inside the Module page. But you are not defining the id in the action parameters. Why? There must be a controller which does the listing of the projects in order to link to the details?

@nadar

<?php

namespace app\modules\projects\frontend\controllers;

use luya\web\Controller;
use yii\data\ActiveDataProvider;
use app\modules\projects\models\Projects;

class DefaultController extends Controller
{
    
    public function actionDetail($id = null)
    {
        $model = Projects::findOne($id);

        if (!$model) {
            return $this->goHome();
        }

        return $this->render('detail', [
            'model' => $model
        ]);
    }
}

Yes, but where is the $id defined? you should define it here

otherwise you need an index action where you can link from index to detail (from overview to detail) So you should make an index action which lists all your projects, and then link into the detail action from the list.

Like this: https://github.com/luyadev/luya-module-news/blob/master/src/frontend/controllers/DefaultController.php#L32-L50

Then you integrate the actionIndex as module page.

When i add action parameters id null in cms page, url working only for project/x and not working for project?id=x. Without parameters working for project/x and project?id=x.

I don’t need index action, i have project list in block on my homepage but detail is on another view.

I think Its problem only in langSwitcher.

$rule is empty

So it only does not work for pl language? Then the problem might be because of:

'composition' => [
    'pl' => 'projekty/<id:\d+>', <--- This prefix does not work it must be `projects` because its the name of the module which need to  be prefixed
    'en' => 'projects/<id:\d+>',
],

You don’t need a multi lingual route here then, just integrate the module also for language PL and remove the composition informations in your url definition.

If you say its a problem from the LangSwitch widget, then please dump $rule to check whether he can evaluate the current url or not.

url is good but problem is with params

langswitcher generate -> image

my url is -> image

So langswitcher didnt add “id” “/35”

Maybe its my fault, but it seems i don’t understand something. Sometimes you mean pl/project then its about ?id=1. I totally see whats your problem regarding url, you want en/project/<id> and pl/projekty/id, if not explain it clearly. I think there are few things strange. I can not say whether its a problem from the lang switch (might be) or from how you build things.

So try the following:

  • Generate a Module Page for all your languages /en/projects and pl/projekty (this is determined by the cms, not a controlller route, its the name of your page in pl translation) and add the module to those page (make sure you disable strict rendering in cms settings!).
  • Since you don’t need multilingual urls add a basic url rule.
 public $urlRules = [
        [
            'pattern' => 'projects/<id:\d+>',
            'route' => 'projects/default/detail',
        ],
    ];

setter was used after getter, thats why i think rule is null

when i set this rule for test (static array) in langswitcher, all links works

ok now i see the problem, when i used langswitcher in blocks there is the problem, on layout/main.php it works

Glad that has been fixed, placing the language switcher in the layout is definitely recommended :+1:

I wonder for a moment, the problem is how we want to use different block-wrapped navbars. I have a module in which I use the cmsPage plugin and in the “detail” view it renders a given page, which causes a problem with this set / get rule. What do you think about it? Can it be fixed somehow? Thanks @nadar

We have never used layout elements in blocks, you should put them into cmslayouts and then select the cmslayout for the given page. To configure certain stuff we use Config::get (https://luya.io/api/luya-admin-models-Config) there you can configure values in the admin in ready them in the fronted (scalar values only).

Maybe we could change the order here: https://github.com/luyadev/luya-module-cms/blob/2ee34950d8ac3662436d09b93c6854a04caa911b/src/models/NavItemModule.php#L133-L137 but needs to verified first. Would you please make an issue? (you can refer to this forum topic)

oh yes, we have cmslayout. I will try to fix it for blocks and then make an issue. Thanks