0

詳細テンプレートを使用しています。 SiteControllerで私のすべてのアクションを作成しました。私のURLはすべてdomain.com/site/somethingなので、urlから "site"という単語を削除してdomain.com/somethingにする必要があります。Yii2:コントローラーをURLから削除する

私はthis question

'urlManager' => [ 
     'class' => 'yii\web\UrlManager', 
     'showScriptName' => false, 
     'enablePrettyUrl' => true, 
     'rules' => array(
       '/<action:\w+>/<id:\d+>' => 'site/<action>', 
       '/<action:\w+>' => 'site/<action>', 
       '/noticia/<slug>' => 'site/noticia', 
     ), 
    ], 

に基づいて、次の規則を試してみましたがまたthis other questionに基づいて、これを試してみました:

'urlManager' => [ 
     'class' => 'yii\web\UrlManager', 
     'showScriptName' => false, 
     'enablePrettyUrl' => true, 
     'baseUrl' => 'http://localhost/websites/transcita/app/frontend/web', 
     'rules' => array(
       [ 
        'pattern' => '<action:\w+>', 
        'route' => 'site/<action>' 
       ], 
       [ 
        'pattern' => '<action:\w+>/<id:\d+>', 
        'route' => 'site/<action>' 
       ], 
       '/noticia/<slug>' => 'site/noticia', 
     ), 
    ], 

が、どちらも動作していません。 domain.com/somethingと入力すると404が表示されます。 私も最初の/なしで試してみましたが、どちらもうまくいきませんでした。

どのような考えですか?

答えて

3

で試してみてください:

'urlManager' => [ 
    'enablePrettyUrl' => true, 
    'showScriptName' => false, 
    'enableStrictParsing' => false, 
    'rules' => [ 

     // ... 

     // last rule 
     '<action:(.*)>' => 'site/<action>', 
    ], 
], 
+0

はあなたに感謝!これは完全に機能しました!その変更がトリックをしたので、私は\ w +と(。*)の違いを読むことができるリンクを持っていますか? –

+1

ここでは、reg exprの優れた説明を見つけることができます:https://regex101.com/。 \ w +は任意の文字、数字またはアンダースコアにマッチします。* 0個以上の文字をマシーンします。たぶんあなたの行動は複数の単語で構成されています。 –

2

別の方法を:

'rules' => [ 
    '<alias:\w+>' => 'site/<alias>', 
], 
+1

この方法は、最初の答えが他のすべてのコントローラ(SiteControllerではなく)に対して404エラーを与えるため、 – Skav

関連する問題