FuelPHP フレームワーク超入門


みなさん今年はイイコトありましたか?さて、前々回のテーマ FuelPHP フレームワークをインストール に続いて、今回はその使い方を見ていきます。 まだ FuelPHP が入っていない人は上記ブログを見てインストールしてみてね。

前回インストールした段階でアクセスすると、以下のように Welcome! 画面が出ました。

fueltop

 

では、今回は /index.php/welcome/hello にアクセスしてみてください。次の画面が出るはずです。

fuelphp_hello

みんな大好き Hello, World !  まず FuelPHP のこの仕組みを理解する必要がありますので見ていきましょう。

まず、ドキュメントルートにアクセスした際、Welcome! ページが出たカラクリは以下のファイルにあります。

(/usr/local/hogeblog/fuel/app/config/routes.php )

<?php
return array(
        '_root_'  => 'welcome/index',  // The default route
        '_404_'   => 'welcome/404',    // The main 404 route

        'hello(/:name)?' => array('welcome/hello', 'name' => 'hello'),
);


3行目はドキュメントルートにアクセスがあったら、「/usr/local/hogeblog/fuel/app/views/welcome/index.php」をみせるという意味、
4行目は、存在しないアクセスがあったら、「/usr/local/hogeblog/fuel/app/views/welcome404.php」をみせるという意味です。
つまり、ドキュメントルートへのアクセスはここをいじれば自分の作ったコンテンツへ向けることができるようになります ´ω`)ノ

次は「/index.php/welcome/hello」にアクセスすると Hello, World !  がでるカラクリをみていきます。

FuelPHP では index.php の次に指定したものが コントローラー となります。

上の例で言うと /index.php/welcome/ の部分ですね。どこやそのファイルというと
fuel/app/classes/controller/welcome.php です。こいつが MVC の Controller そのもの ↓

/**
 * The Welcome Controller.
 *
 * A basic controller example.  Has examples of how to set the
 * response body and status.
 *
 * @package  app
 * @extends  Controller
 */
class Controller_Welcome extends Controller
{        /**
         * The basic welcome message
         *
         * @access  public
         * @return  Response
         */
        public function action_index()
        {
                return Response::forge(View::forge('welcome/index'));
        }

        /**
         * A typical "Hello, Bob!" type example.  This uses a Presenter to
         * show how to use them.
         *
         * @access  public
         * @return  Response
         */
        public function action_hello()
        {
                return Response::forge(Presenter::forge('welcome/hello'));
        }
        /**
         * The 404 action for the application.
         *
         * @access  public
         * @return  Response
         */        public function action_404()        {                return Response::forge(Presenter::forge('welcome/404'), 404);        }
}

クラス名やメソッド名は FuelPHP に厳格な命名規則があります。今はこういうものだと流してください。Welcome コントローラーが中で何をやっているかというと

・17行目 welcome/index にアクセスがあったら、welcome/index.php をみせるんやで、という指示が書いてあります。action_●●で指定できます。

・29行目  welcome/hello にアクセスがあったら、 welcome/hello.php をみせるん(略)

つまり、通常のアクセスは 「/index.php/コントローラー名/メソッド名/メソッドで指定したファイル」で受け取るということです。

つまり、通常のアクセスは 「/index.php/コントローラー名/メソッド名/メソッドで指定したファイル」で受け取るということです。

大事なことなので2回言いました。

でも、毎回 index.php つけるのダサいですよね。これを消す方法をみていきましょう。

 FuelPHP で index.php を消す方法

実はめちゃかんたんです。以下のファイルの 27~29行目のコメントアウトを外して Apache を再起動してください。
(/usr/local/hogeblog/public/.htaccess)

# Multiple Environment config, set this to development, staging or production
# SetEnv FUEL_ENV production

<IfModule mod_rewrite.c>        RewriteEngine on

        # NOTICE: If you get a 404 play with combinations of the following commented out lines
        #AllowOverride All
        #RewriteBase /wherever/fuel/is

    # Make sure directory listing is disabled        Options +FollowSymLinks -Indexes

        # Restrict your site to only one domain
        # !important USE ONLY ONE OPTION

        # Option 1: To rewrite "www.domain.com -> domain.com" uncomment the following lines.
        #RewriteCond %{HTTPS} !=on
        #RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
        #RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

        # Option 2: To rewrite "domain.com -> www.domain.com" uncomment the following lines.
        #RewriteCond %{HTTPS} !=on
        #RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
        #RewriteCond %{HTTP_HOST} (.+)$ [NC]
        #RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

        # Remove index.php from URL
        RewriteCond %{HTTP:X-Requested-With}    !^XMLHttpRequest$
        RewriteCond %{THE_REQUEST}                              ^[^/]*/index\.php [NC]
        RewriteRule ^index\.php(.*)$                    $1 [R=301,NS,L]

Apache で .htaccess による設定上書きを有効にしていない方は以下を変更
(/etc/httpd/conf/httpd.conf)

<Directory />
    Options FollowSymLinks
    #AllowOverride None
    AllowOverride All   # None → All
</Directory>

これで、index.php を入れなくてもアクセスできるようになっているはず・・・
fuelphp_nonphp

↑ イケましたね。これで MVC のコントローラーからビューへの受け渡しができます。

いかがでしたか?
FuelPHP の超入門ということで C(コントローラー)と V(ビュー)の使い方をみてきました。
実際は DB との連携でアプリケーションを作りますので、またの機会に FuelPHP の便利な M (モデル)の使い方をみていきたいと思います。

それではみなさん良いお年をー( ´ω`)ノ


“FuelPHP フレームワーク超入門” への1件のフィードバック

  1. […] FuelPHP フレームワーク超入門 […]

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>