RouteBehaviorUrlRule

Hello, what is the best way to remove RouteBehaviorUrlRule from url rules? I want to process only page routes and disable direct access to modules routes.
For example I have module ‘blog’, but want /blog/ path to point to page, not to module directly.
(of course I can rename the module, but it still will be accessible by path with module’ name what is not desirable)

I tried to remove the event handler for Application::EVENT_BEFORE_REQUEST (where the rule is set) during luyaBootstrap() in my module, but luya\cms\frontend\Bootstrap::bootstrap() goes later than other module’s bootstraps and so the rule can’t be removed by such way.

I think maybe better to make RouteBehaviorUrlRule aware ‘enableStrictParsing’ => true setting in urlManager?

Because in Yii this setting stops autoresolve module/controller/action to modules if this route is not set explicitly via rules.
Same can be with Luya, if ‘enableStrictParsing’ is set then don’t resolve module routes if a route is not set in rules.

Hey @ms177 i have to admin, i can not “ultimately” answer your question, i need to test out things as well. I think if you remove that rule, the loading of CMS modules won’t work anymore. For testing purposes, you can just remove that url rule from your vendor folder by editing https://github.com/luyadev/luya-module-cms/blob/master/src/frontend/Bootstrap.php#L41 - if your desired result works afterwards, we can find a solution i would say :+1:

But you need to test with modules. Add a module page and add a module block, etc.

Does that help?

Hi @nadar, yep I tested it.
Without RouteBehaviorUrlRule all is working. The rule is working only not in admin context and has parsing mode so it shouldn’t affect anything if main url is not module/controller/action.

Also frontend is working well with enableStrictParsing and without RouteBehaviorUrlRule. I used this code:

        if ($app->hasModule('cms')) {
            if ($app->urlManager->enableStrictParsing) {
                $urlRules = [
                    ['class' => 'luya\cms\frontend\components\WebsiteBehaviorUrlRule'],
                    ['class' => 'luya\cms\frontend\components\CatchAllUrlRule'],
                ];

            // not strict parsing
            // add RouteBehaviorUrlRule to auto-resolve <module>/<controller>/<action> pattern
            } else {
                $urlRules = [
                    ['class' => 'luya\cms\frontend\components\WebsiteBehaviorUrlRule'],
                    ['class' => 'luya\cms\frontend\components\RouteBehaviorUrlRule'],
                    ['class' => 'luya\cms\frontend\components\CatchAllUrlRule'],
                ];
            }

            // load cms url rules
            $app->on(Application::EVENT_BEFORE_REQUEST, function ($event) use ($urlRules) {
                if (!$event->sender->request->isConsoleRequest && !$event->sender->request->isAdmin) {
                    $event->sender->urlManager->addRules($urlRules);
                }
            });

            // handle not found exceptions
...

But admin is not working with enableStrictParsing as it lacks some rules for it, for example admin/login/async is not being resolved and gets 404.

So if to use strict parsing all needed routes for admin actions should be added to rules then.

Or some new config setting can be used to disable RouteBehaviorUrlRule.

Also while we are at it, it would be great to be able to disable WebsiteBehaviorUrlRule via configs too, if redirect functionality is not needed.

It will increase performance as WebsiteBehaviorUrlRule performs DB query for every request.

yes, the admin won’t work with strict parsing. I will do some tests, maybe we can adjust the url rules which are loaded for certain circumstances.

@boehsermoe Maybe we could introduce a flag for WebsiteBehaviorUrlRule to disable that feature, as 90% of all pages never use that feature of multiple domains but need to take the cost of that db requests. Then we could provided this information in the performance guide - if you don't need multiple websites, turn XYZ settings off.

1 Like

Good point. I started a new PR option to disable WebsiteBehaviorUrlRule by boehsermoe · Pull Request #354 · luyadev/luya-module-cms · GitHub

1 Like