2016-04-12 11 views
0

私はControllerを拡張するMainControllerを持っています。私のすべてのアプリケーションのコントローラは、MainControllerから拡張されています。MainControllerには、さまざまなメソッドやプロパティがあり、コントローラからアクセスできる必要があります。データベースで開催されたリダイレクトのためのYii2はbeforeActionメソッド内でrunAction()を使用します

  • かどうかをチェックし、URLがDB内の1つの一致した場合、それらを実行します。私のメインコントローラ内

    には、いくつかのことを行いいる、beforeActionです。

  • コントローラごとにデータを生成します。
  • URLのクッキーとスラッグに基づいて、ユーザーが探している言語と国を取得します。 (すなわち、http://example.com/netherlands)。
  • URLがデータベースのpagesテーブルのものと一致する場合、テンプレートから汎用ページをレンダリングします。

これは私が苦労している最後のものです。

/** 
* Before action, check all $this->before_actions satisfy. If no head_data provided, try and fill in some basics 
*/ 
public function beforeAction($action) 
{ 

    // Run parent method first 
    if (!parent::beforeAction($action)) 
     return false; 

    // Check redirects 
    $this->checkRedirects(); 

    if($this->checkPages()) 
    { 

     // If not error page, loop through before methods 
     if($action->id !== 'error') 
     { 

      // Loop through actions to peform and do them 
      foreach ($this->before_actions as $before_method) 
       $this->$before_method(); 
     } 
     return true; 
    } 

} 

ます$ this-> checkPages()次の内容が含まれます:

/** 
* Check for pages 
*/ 
public function checkPages() 
{ 

    // Attempt to find page for this request 
    $page = Page::find()->where([ 'permalink' => trim(str_replace(getBaseUrl() , "", getCurrentUrl()), "/") ])->one(); 

    // If found, load it instead 
    if(!empty($page)) 
     return Yii::$app->runAction("pages/show", [ 'id' => $page->id ]); 

    // Else, return 
    return true; 

} 

なしがありますので、私が午前問題は、私がhttp://example.com/storyに行けばということである私のメインコントローラでは、私はこれを持っていますStoryControllerでは、アクションは実行され、ビュー "views/story/show"が出力されますが、404エラーが返されます。

どうすればこの問題を防ぐことができますか?

EDIT: "Unable to resolve the request 'story/index'"を:

追加するには、ログは、それが最初に言うことを示しています。

しかし、その後、追加のログ・ショー: "Route to run: pages/show" ... "Running action: app\controllers\PagesController::actionShow()" .. Rendering view file: /Users/stefandunn/Documents/Local Machine Development/views/pages/show.php

だから私は404のステータス

答えて

0

は、いずれかをキャッチすることができます1つの最後のルートを追加引き起こし、それが最初のログ結果だ推測していますパターンを作成し、カスタムアクションにリダイレクトします。

'urlManager' => [ 
    'class' => 'yii\web\UrlManager', 
    'enablePrettyUrl' => true, 
    'showScriptName' => false, 
    'rules' => [ 
     //... 
     '<any:.*>' => 'site/index' 
    ], 
], 
+0

私は、しかし、私は標準的な経路 '/を'キャッチする最後のルールを使用して、提案された解決策として、これを検討していました。私はページの作成/更新/削除時にhtaccessファイルを書き直すことになった。 –

関連する問題