2017-11-26 1 views
0

私はこのチュートリアルでルーティングがどのように動作するかをどのように変更できるかを理解しようとしています。ルーティングメカニズムでURLの一部を削除するにはどうすればよいですか?

あなたは、たとえばコンタクトページに行けば同じよう現在のところ、URLが見えます:私は「ページと呼ばれる別のフォルダとビューのフォルダを持っていることを理解し

mysite.com/pages/contact

「これで、私はそのビューにデータを送り、ページコントローラがあります。

$this->view('pages/contact', $data); 

をしかし、私はそれが見えるようにしたくありません。私はむしろmysite.com/contactでしたが、/ pages/

私は私が役立つと思うものを投稿するつもりであるかどうかわかりません。私のindex.phpと私のルートフォルダに

私は私のパブリックフォルダでの.htaccessファイル

<IfModule mod_rewrite.c> 
RewriteEngine on 
RewriteRule ^$ public/ [L] 
RewriteRule (.*) public/$1 [L] 
</IfModule> 

を持って、私は別の.htaccessの私のページコントローラで

<IfModule mod_rewrite.c> 
Options -Multiviews 
RewriteEngine On 
RewriteBase /jonomvc/public 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] 
</IfModule> 

を持っている私が持っている:

public function contact() { 

     $data = [ 

      'title' => 'Contact us' 
     ]; 

     $this->view('pages/contact', $data); 
} 

これは私のコアクラスです:

class Core { 

    protected $currentController = 'Pages'; 
    protected $currentMethod = 'index'; 
    protected $params = []; 

    public function __construct() { 
     $url = $this->getUrl(); 

     // Look in Controllers for first value 
     if(file_exists('../app/controllers/' . ucwords($url[0]) . '.php')) { 
      //if exists, set as controller 
      $this->currentController = ucwords($url[0]); 
      // Unset zero index 
      unset($url[0]); 
     } 

     // Require the controller 
     require_once '../app/controllers/' . $this->currentController . '.php'; 

     // Instantiate controller 
     $this->currentController = new $this->currentController; 

     // Check for second part of url 
     if(isset($url[1])) { 

      $url[1] = str_replace('-', '_', $url[1]); 

      // check to see if method exists in controller 
      if(method_exists($this->currentController, $url[1])) { 
       $this->currentMethod = $url[1]; 
       // Unset 1 index 
       unset($url[1]); 
      } 
     } 

     // Get params 
     $this->params = $url ? array_values($url) : []; 

     // Call a callback with array of params 
     call_user_func_array([$this->currentController, $this->currentMethod], $this->params); 
    } 

    public function getUrl() { 
     if(isset($_GET['url'])) { 
      $url = rtrim($_GET['url'], '/'); 
      $url = filter_var($url, FILTER_SANITIZE_URL); 
      $url = explode('/', $url); 
      return $url; 
     } 

    } 
} 

私はコンタクトページは、私の見解」フォルダの中に、私の「ページ」フォルダの中に座っていた場合、私が持っているだろう「Contact.php」

class Contact extends Controller { 

    public function __construct() { 


    } 

    public function contact() { 

     $data = [ 

      'title' => 'Contact us' 
     ]; 

     $this->view('contact', $data); 
    } 
} 

と呼ばれるコントローラのフォルダ内にコントローラを作成使用:私は/ページ/一部たくないので、

$this->view('pages/contact', $data); 

しかし、私が代わりに見解/ページ」フォルダの見解」フォルダのルートにcontact.phpを置くが、それはありません例:

$this->view('contact', $data); 

エラーは次のとおりです。

警告:パラメータ1が有効なコールバックであることを期待call_user_func_array()、クラスの連絡先は「/アプリケーションメソッド「インデックス」を持っていない/ MAMP/htdocsに/個人用サイト/アプリライン上の/libraries/Core.php 54

ライン54は:

call_user_func_array([$this->currentController, $this->currentMethod], $this->params); 
+0

停止)をロードする必要があること言います。代わりに正規表現を使用してください([this post](https://stackoverflow.com/a/19309893/727208)で説明されています)。ここで、それぞれのマッチするルートに対して、「デフォルト」または「サイレント」のパラメータを設定できます。または、オプションの場合は、[this](http://symfony.com/doc/current/components/routing.html)や[this](https://github.com/nikic)のようなスタンドアロンルーティングコンポーネントを使用する必要があります。/FastRoute)。 –

答えて

0

CodeIgniterでは、アプリケーションであなたののroutes.phpファイルを設定するには>設定とし、単にthisを実行します。 イメージを参照してください。単にcontrollername/filenameが呼び出された場合は、controllername/filenameの代わりにファイル名のみを表示するようにしてください。

第2に、でやりたい場合。htaccessファイルファイルはちょうど以下の操作を行います。

RewriteRule ^new/registration?$ registration.php 

をこれがmysite.com/new/registrationが呼び出された場合、あなたはPARS URLに ``(爆発に中継registration.php

+0

あなたは答えをより読みやすく、応援してくれるように、常に[formatting](https://stackoverflow.com/help/formatting)を使うべきです –

関連する問題