ステータス:非推奨

この記事では、サポートされなくなったバージョンのUbuntuについて説明します。 現在Ubuntu12.04を実行しているサーバーを運用している場合は、サポートされているバージョンのUbuntuにアップグレードまたは移行することを強くお勧めします。

理由:
Ubuntu 12.04は2017年4月28日に保守終了(EOL)に達しました and no longer receives security patches or updates. This guide is no longer maintained.

代わりに参照してください:
このガイドは参照として役立つ場合がありますが、他のUbuntuリリースでは機能しない場合があります。 可能な場合は、使用しているUbuntuのバージョン用に作成されたガイドを使用することを強くお勧めします。 ページ上部の検索機能を使用して、より新しいバージョンを見つけることができます。

ファルコンについて


Phalcon は、Model-View-Controllerアーキテクチャを促進するPHPフレームワークであり、ORM、テンプレートエンジン、ルーティング、キャッシングなど、このようなソフトウェアに期待される多くのフレームワークのような機能を備えています。

それについての1つのクールな点は、パフォーマンスの面で、他のフレームワークよりも間違いなくはるかに高速であることです。 その理由は、ファイルをサーバーにコピーするだけの通常のPHPフレームワークではなく、問題がないためです。 実際には、Cで記述されたPHP拡張機能です。

この記事では、Ubuntu12.04を実行しているVPSでPhalconを使い始める方法を見ていきます。 フォローしている場合は、サーバーがすでにLAMPスタック(Apache、MySQL、PHP)でセットアップされていると思います。 DigitalOcean には、必要に応じてセットアップするのに役立つ優れたチュートリアルがあります。

インストール


最初に行う必要があるのは、Phalconの要件をインストールすることです。 これを行うには、次の3つのコマンドを実行します。

sudo apt-get update
sudo apt-get install git-core gcc autoconf make
sudo apt-get install php5-dev php5-mysql

You can remove packages from the commands if you already have them installed (for instance git-core if you already have Git installed). Next up, you need to clone the framework repo onto your system:

git clone git://github.com/phalcon/cphalcon.git

After that is done, navigate in the following folder with this command:

cd cphalcon/build

And run the install file to install the extension:

sudo ./install

What you need to do next is edit your php.ini file:

nano /etc/php5/apache2/php.ini

And add the following line to the end of it:

extension=phalcon.so

Then restart your server for the changes to take effect:

sudo service apache2 restart

And this should do it. To check if Phalcon has been successfully installed, you’ll need to check the output of phpinfo() statement. If you don’t know how to proceed, create a file called info.php somewhere where you can access it from the browser and paste in the following line:

<?php phpinfo(); ?>

Save the file and point your browser to it. In the PHP information displayed on that page, you should see that the Phalcon framework is enabled and also verify its version.

最初のPhalconプロジェクト構造


他のPHPフレームワークを使用したことがある場合は、プロジェクトのフォルダー構造のどこかにフレームワーク関連のファイルがあることを期待しているでしょう。 Phalconを使用すると、これらのファイルはすべてメモリ内ですぐに利用できるため、開始するために必要なのは、Apacheのドキュメントルート(デフォルトは / var / www )のどこかに空のフォルダー構造を作成することだけです。 推奨される方法は次のとおりです。

project_name/
 app/
   controllers/
   models/
   views/
 public/
   css/
   img/
   js/

So what you have here is a project folder which has 2 main folders: アプリ and 公衆. The first will house the logic of your application (mostly PHP) whereas the second one is where your browser will point and be redirected to the resources in the app folder on the one hand, and have access to all the frontend assets, on the other.

ブートストラップ


作成する必要のある最初の最も重要なファイルは、アプリケーションがブートストラップに使用するindex.phpファイルです。 アプリケーションのpublic/フォルダーに次のファイルを作成します。

nano /var/www/project_name/public/index.php

And paste in the following code:

<?php

try {

   //Register an autoloader
   $loader = new \Phalcon\Loader();
   $loader->registerDirs(array(
       '../app/controllers/',
       '../app/models/'
   ))->register();

   //Create a DI
   $di = new Phalcon\DI\FactoryDefault();

   //Setup the view component
   $di->set('view', function(){
       $view = new \Phalcon\Mvc\View();
       $view->setViewsDir('../app/views/');
       return $view;
   });

   //Setup a base URI so that all generated URIs include the "tutorial" folder
   $di->set('url', function(){
       $url = new \Phalcon\Mvc\Url();
       $url->setBaseUri('/project_name/');
       return $url;
   });

   //Handle the request
   $application = new \Phalcon\Mvc\Application($di);

   echo $application->handle()->getContent();

} catch(\Phalcon\Exception $e) {
    echo "PhalconException: ", $e->getMessage();
}

For more information about what this file contains, you can check the official Phalcon website. But please note that you need to replace this line:

$url->setBaseUri('/project_name/');

With one that is appropriate for your case, i.e. containing the name of your project folder.

URL書き換え


Phalconは、 .htaccess ファイルを使用して、重要な再ルーティングを行い、アプリケーションのフォルダー構造を詮索好きな目から保護する必要があります。 このためには、Apacheの mod_rewrite モジュールを有効にし、.htaccessファイルでApache命令を変更できるようにする必要があります。

したがって、これが当てはまらない場合は、Phalconアプリケーションが存在するApache仮想ホストファイルを編集します(デフォルトは / var /wwwです。このアプリケーション用の特定の仮想ホストはありません)、 AllowOverrides/var /wwwディレクトリの下のAllに設定されていることを確認してください(ここでも、アプリケーションがデフォルトのApacheドキュメントルートにある場合)。 次のコマンドを使用して、デフォルトの仮想ホストファイルを編集できます。

nano /etc/apache2/sites-available/default

And where you see this block, make the changes to correspond to the following.

<Directory /var/www/>
   Options Indexes FollowSymLinks MultiViews
   AllowOverride All
   Order allow,deny
   allow from all
</Directory>

Finally, make sure that mod_rewrite is enabled in your Apache. To check if it is already enabled, use the following command:

apache2ctl -M

If you see “rewrite_module” in the list, you are fine. If not, use the following command to enable the module:

a2enmod rewrite

After all these steps, or after any individual one you had to perform, give Apache a restart so they take effect:

sudo service apache2 restart

Now that this is taken care of, create an .htaccess file in the main project folder:

nano /var/www/project_name/.htaccess

And paste in the following directives:

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

This will reroute all requests from the project main folder to the public/ folder. Now create another .htaccess file but this time in the public/ folder:

nano /var/www/project_name/public/.htaccess

And paste in the following directives:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

This will reroute all the requests coming to this folder to the index.php file (in case an actual file by the name requested does not exist in that folder or below – you know, for the frontend assets to still be accessible).

あなたの最初のコントローラー


ブラウザでプロジェクトフォルダを指定すると、インデックスコントローラクラスを読み込めなかったというエラーが表示されます。 これは、リクエストで渡されたコントローラーがない場合、デフォルトでアプリケーションがこれらのいずれかを必要とするためです。 それでは、 HelloWorldをページに表示するものを作成しましょう。

コントローラーフォルダーに次のIndexController.phpファイルを作成します。

nano /var/www/project_name/app/controllers/IndexController.php

中に、以下を貼り付けます。

<?php

class IndexController extends \Phalcon\Mvc\Controller {

   public function indexAction()    {
       echo "<h1>Hello World!</h1>";
   }

}

You’ll notice that we extend the default Phalcon Controller class and name it IndexController (all controller names need to end with the word “Controller”. Inside this class, we define a method (all methods, also called Actions, need to end with the word “Action”) called indexAction. Since it is called index, it is also the first Action that gets called by this controller if no particular action is specified in the request.

If you now access the project folder from the browser, you should see the string being echoed.

結論


このチュートリアルでは、Phalconをインストールしてプロジェクトを開始する方法を見てきました。 次の記事では、もう少し詳しく説明し、Phalcon MVCフレームワークを活用する方法と、データベースに格納されている情報にアクセスする方法を確認します。

投稿者: Danny