Symfony2のアプリ開発1

前回までは開発環境の説明をしましたが、今回からやっとアプリの開発をしていきます。
開発をする前に以下が開発環境となります。

・MAMP2.1.1 MAMP & MAMP PRO - your local web development solution for PHP and WordPress development
Aptana Studio3.2.2 Standalone Version Aptana
MAMPに付属のphp5.3(5.4でもいいよ)


これさえあれば開発環境は整います。昔に比べたら非常に楽な世の中になりましたね。
ちなみにWindowsの方はXAMPPを使ってください。
おいらも昔はMacのXAMPPを使っていましたが、
バージョンアップがWindowsに比べて遅いのでMAMPに乗り換えました。


早速ですがアプリの開発をしていきましょう。
前回id:amidaike:20121205はSymfonyというプロジェクトをAptanaで作成しました。


次に行うのは今回作成するアプリのバンドルを作ります。
バンドルとは簡単に言うと他の奴でいうプラグインみたいのようなものですね。他の奴てなんだ。。
Symfony2ではこのバンドルが組み合わさって作成されています。
vendorディレクトリにある一つ一つのディレクトリがバンドルとなりますが、
まぁ詳しい話はおいといてこういう物だと覚えてください。


そのバンドルを作るところから始めます。
ターミナルでSymfonyディレクトリまで移動します。
Aptanaの場合、TerminalというWindowが下の方にありますね。
ここね。

ここで、以下のコマンドを入力してください。

php app/console generate:bundle --namespace=App/SampleBundle --format=yml

何かを訪ねられますが気にせずに全てリターンキーを押してください。
こんな内容ですね。

                          
  Welcome to the Symfony2 bundle generator  
                                            


Your application code must be written in bundles. This command helps
you generate them easily.

Each bundle is hosted under a namespace (like Acme/Bundle/BlogBundle).
The namespace should begin with a "vendor" name like your company name, your
project name, or your client name, followed by one or more optional category
sub-namespaces, and it should end with the bundle name itself
(which must have Bundle as a suffix).

See http://symfony.com/doc/current/cookbook/bundles/best_practices.html#index-1 for more
details on bundle naming conventions.

Use / instead of \  for the namespace delimiter to avoid any problem.

Bundle namespace [App/SampleBundle]: 

In your code, a bundle is often referenced by its name. It can be the
concatenation of all namespace parts but it's really up to you to come
up with a unique name (a good practice is to start with the vendor name).
Based on the namespace, we suggest AppSampleBundle.

Bundle name [AppSampleBundle]: 

The bundle can be generated anywhere. The suggested default directory uses
the standard conventions.

Target directory [/Users/ユーザ名/Documents/Aptana Studio 3 Workspace/Symfony/src]: 

Determine the format to use for the generated configuration.

Configuration format (yml, xml, php, or annotation) [yml]: 

To help you get started faster, the command can generate some
code snippets for you.

Do you want to generate the whole directory structure [no]? 

                             
  Summary before generation  
                             

You are going to generate a "App\SampleBundle\AppSampleBundle" bundle
in "/Users/ユーザ名/Documents/Aptana Studio 3 Workspace/Symfony/src/" using the "yml" format.

Do you confirm generation [yes]? 

                     
  Bundle generation  
                     

Generating the bundle code: OK
Checking that the bundle is autoloaded: OK
Confirm automatic update of your Kernel [yes]? 
Enabling the bundle inside the Kernel: OK
Confirm automatic update of the Routing [yes]? 
Importing the bundle routing resource: OK

                                               
  You can now start using the generated code!  

終了するとsrcディレクトリには、まぁ何と言う事でしょう。ファイルが自動生成されているではありませんか。

RoRを経験されている方なら rails new アプリケーション名 を実行したようなもんです。


この後、ブラウザから
http://localhost:8001/app_dev.php/hello/hoge
と入力してください。

すると

Hello hoge!

と表示されましたね。

特に何もせずにバンドルを作成しただけで簡単なアプリが作成されました。
ここまでは凄く簡単でしょう。


表示されない方はバンドルの作成やMAMPのhtdocsの設定やシンボリックリンクがきちんと作成されているかを確認してください。
これでアプリを作成する準備は整いました。

次回は、このバンドル作成についてもう少し説明していきます。