U
U
ukoHka2017-10-24 11:51:08
Yii
ukoHka, 2017-10-24 11:51:08

How to configure routing depending on parameters in UrlManager?

In the texts of articles on the site there are links like index.php?option=com_content&view=article&id=1
How to register in UrlManager so that when such links are opened, a specific controller is opened?
For example, if view = article, then article/view is opened and the id parameter is passed, and if view = category, then category/view

'rules' => [
    'index.php?option=com_content&view=article' = 'article/view',
]
redirects everything to index.php, and if you replace index.php with files.php, it gives 404.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vksee, 2017-10-24
@ukoHka

*** Option 1 *** - create a custom rule for the URL.

<?php
namespace app\components;
use yii\web\UrlRuleInterface;
use yii\base\Object;

class JoomlaUrlRule extends Object implements UrlRuleInterface
{
    public function createUrl($manager, $route, $params)
    {
        return false;
    }

    public function parseRequest($manager, $request)
    {
        $it_joomla_url = $request->getPathInfo()==='' 
                     && $request->getQueryParam('option')==='com_content';
        if (!it_joomla_url) {
            return false;            
        }
        $view = $request->getQueryParam('view');
        if (empty($view)) {
            return false;            
        }
        return ["$view/view", $request->queryParams];
    }
}

Register it in the application config:
[
    'components' => [
        'urlManager' => [
            // ...
            'rules' => [
                ['class' => 'app\components\JoomlaUrlRule'],
                // ...
            ],
        ],
    ],
]

Yii2: Parsing and generating URL
*** Option 2 *** - solve the problem with web server settings.
For the Apache web server, add the following lines to the .htaccess file:
RewriteCond %{THE_REQUEST} index.php [NC]
RewriteCond %{QUERY_STRING} option=com_content [NC]
RewriteCond %{QUERY_STRING} view=category [NC]
RewriteRule (.*) /category/view/ [L,R=301,QSA]

RewriteCond %{THE_REQUEST} index.php [NC]
RewriteCond %{QUERY_STRING} option=com_content [NC]
RewriteCond %{QUERY_STRING} view=article [NC]
RewriteRule (.*) /article/view/ [L,R=301,QSA]

Information about rules...
Это два набора правил редиректа, для ссылок вида:
index.php?option=com_content&view=category...
index.php?option=com_content&view=article...
Для изначального URL запроса - порядок параметров значения не имеет.
К примеру, должны работать варианты:
index.php?option=com_content&view=article&id=1
index.php?id=1&view=article&option=com_content
По ссылке index.php?option=com_content&view=article&id=1
будет редирект на /article/view?option=com_content&view=article&id=1
Все параметры запроса затем передаются в контроллер, обрабатывать можно только нужные параметры, к примеру, только id.

But for SEO , I recommend stricter rules:
RewriteCond %{THE_REQUEST} index.php [NC]
RewriteCond %{QUERY_STRING} option=com_content [NC]
RewriteCond %{QUERY_STRING} view=category [NC]
RewriteCond %{QUERY_STRING} id=(\d+) [NC]
RewriteRule (.*) /category/view?id=%1 [L,R=301]

RewriteCond %{THE_REQUEST} index.php [NC]
RewriteCond %{QUERY_STRING} option=com_content [NC]
RewriteCond %{QUERY_STRING} view=article [NC]
RewriteCond %{QUERY_STRING} id=(\d+) [NC]
RewriteRule (.*) /article/view?id=%1 [L,R=301]

The link index.php?option=com_content&view=article&id=1 will redirect to /article/view?id=1

M
Maxim Timofeev, 2017-10-24
@webinar

$url = Url::to(['post/view', 'id' => 100]);
create
/index.php?r=post%2Fview&id=100

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question