Codeigniter-page-redirection

提供:Dev Guides
移動先:案内検索

CodeIgniter-ページのリダイレクト

Webアプリケーションの構築中に、ユーザーをあるページから別のページにリダイレクトする必要がよくあります。 CodeIgniterを使用すると、この作業が簡単になります。 この目的には* redirect()*関数が使用されます。

Syntax redirect($uri = , $method = 'auto', $code = NULL)
Parameters
  • $ uristring)− URI文字列
  • $ methodstring)−リダイレクト方法(「auto」、「location」、または「refresh」)
  • $ codestring)− HTTP応答コード(通常302または303)
Return type void

最初の引数には、2種類のURIを指定できます。 完全なサイトURLまたはURIセグメントを、指示するコントローラーに渡すことができます。

2番目のオプションパラメータには、auto、location、またはrefreshの3つの値のいずれかを指定できます。 デフォルトは自動です。

3番目のオプションパラメータは、場所のリダイレクトでのみ使用でき、特定のHTTP応答コードを送信できます。

*Redirect_controller.php* というコントローラーを作成し、 *application/controller/Redirect_controller.php* に保存します
<?php
   class Redirect_controller extends CI_Controller {

      public function index() {
        /*Load the URL helper*/
         $this->load->helper('url');

        /*Redirect the user to some site*/
         redirect('http://www.finddevguides.com');
      }

      public function computer_graphics() {
        /*Load the URL helper*/
         $this->load->helper('url');
         redirect('http://www.finddevguides.com/computer_graphics/index');
      }

      public function version2() {
        /*Load the URL helper*/
         $this->load->helper('url');

        /*Redirect the user to some internal controller’s method*/
         redirect('redirect/computer_graphics');
      }

   }
?>
*application/config/routes.php* の *routes.php* ファイルを変更して、上記のコントローラーのルートを追加し、ファイルの最後に次の行を追加します。
$route['redirect'] = 'Redirect_controller';
$route['redirect/version2'] = 'Redirect_controller/version2';
$route['redirect/computer_graphics'] = 'Redirect_controller/computer_graphics';

ブラウザーに次のURLを入力して、例を実行します。

http://yoursite.com/index.php/redirect

上記のURLは、finddevguides.com Webサイトにリダイレクトします。次のURLにアクセスすると、finddevguides.comのコンピュータグラフィックチュートリアルにリダイレクトされます。

http://yoursite.com/index.php/redirect/computer_graphics