Symfony2チュートリアル/01/


概要

Symfony2を勉強していてなかなか最近のものに当てはまりそうなものがないので、古いやつをエラーを解決しながら進めていく。

参照サイト
http://jobeet.symfony.gr.jp/day-1-starting-up-the-project.html

目次

  • Symfonyをダウンロードとインストール
  • Vendorsの更新
  • サーバーを立ち上げてみる

Symfonyをダウンロードとインストール

サイトから取得したコマンドからsymfonyプロジェクトを作る。my_projectの部分をjobeetに変更する。

お好みのディレクトリ下で下記のコマンドを実行する。

$ symfony new jobeet 2.8

 Downloading Symfony...

    5.4 MiB/5.4 MiB ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓  100%

 Preparing project...

 ✔  Symfony 2.8.21 was successfully installed. Now you can:

    * Change your current directory to /Users/workspace/jobeet

    * Configure your application in app/config/parameters.yml file.

    * Run your application:
        1. Execute the php app/console server:start command.
        2. Browse to the http://localhost:8000 URL.

    * Read the documentation at http://symfony.com/doc$ symfony new jobeet 2.8

 Downloading Symfony...

    5.4 MiB/5.4 MiB ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓  100%

 Preparing project...

 ✔  Symfony 2.8.21 was successfully installed. Now you can:

    * Change your current directory to /Users/workspace/jobeet

    * Configure your application in app/config/parameters.yml file.

    * Run your application:
        1. Execute the php app/console server:start command.
        2. Browse to the http://localhost:8000 URL.

    * Read the documentation at http://symfony.com/doc

Vendors の更新

Symfony のプロジェクトは、多くの外部ライブラリに依存しており、Composer と呼ばれるライブラリを経由して、プロジェクトの vendor/ ディレクトリにダウンロードされる。まずはComposerからダウンロードする。

$ curl -s https://getcomposer.org/installer | php

All settings correct for using Composer
Downloading...

Composer (version 1.4.2) successfully installed to: /Users/workspace/jobeet/composer.phar
Use it: php composer.phar
$ php composer.phar install

All settings correct for using Composer
Downloading...

Composer (version 1.4.2) successfully installed to: /Users/workspace/jobeet/composer.phar
Use it: php composer.phar

Takaharu-no-MacBook-Pro:jobeet$ php composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
> Incenteev\ParameterHandler\ScriptHandler::buildParameters
Updating the "app/config/parameters.yml" file
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache

 // Clearing the cache for the dev environment with debug true                                                          


 [OK] Cache for the "dev" environment (debug=true) was successfully cleared.                                            


> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets

 Trying to install assets as relative symbolic links.

 --- -------------------------- ------------------ 
      Bundle                     Method / Error    
 --- -------------------------- ------------------ 
  ✔   FrameworkBundle            relative symlink  
  ✔   SensioDistributionBundle   relative symlink  
 --- -------------------------- ------------------ 


 [OK] All assets were successfully installed.                                                                           


> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget

サーバーを立ち上げてみる

$ php app/console server:run

 [OK] Server running on http://127.0.0.1:8000   

http://127.0.0.1:8000にアクセスして下記のような画面が出ればOK。

Application Bundle の作成

Symfony のバンドルジェネレーターを開始するには、次のコマンドを実行する。

$ php app/console generate:bundle --namespace=Ibw/JobeetBundle


  Welcome to the Symfony bundle generator!  


Are you planning on sharing this bundle across multiple applications? [no]: no

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

Give your bundle a descriptive name, like BlogBundle.
Bundle name [Ibw/JobeetBundle]: 

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 IbwJobeetBundle.

Bundle name [IbwJobeetBundle]: IbwJobeetBundle

Bundles are usually generated into the src/ directory. Unless you're
doing something custom, hit enter to keep this default!

Target Directory [src/]: src/

What format do you want to use for your generated configuration?

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


  Bundle generation  


> Generating a sample bundle skeleton into app/../src/Ibw/JobeetBundle
  created ./app/../src/Ibw/JobeetBundle/
  created ./app/../src/Ibw/JobeetBundle/IbwJobeetBundle.php
  created ./app/../src/Ibw/JobeetBundle/Controller/
  created ./app/../src/Ibw/JobeetBundle/Controller/DefaultController.php
  created ./app/../tests/IbwJobeetBundle/Controller/
  created ./app/../tests/IbwJobeetBundle/Controller/DefaultControllerTest.php
  created ./app/../src/Ibw/JobeetBundle/Resources/views/Default/
  created ./app/../src/Ibw/JobeetBundle/Resources/views/Default/index.html.twig
  created ./app/../src/Ibw/JobeetBundle/Resources/config/
  created ./app/../src/Ibw/JobeetBundle/Resources/config/services.yml
  created ./app/../src/Ibw/JobeetBundle/Resources/config/routing.yml
> Checking that the bundle is autoloaded
> Enabling the bundle inside app/AppKernel.php
  updated ./app/AppKernel.php
> Importing the bundle's routes from the app/config/routing.yml file
  updated ./app/config/routing.yml
> Importing the bundle's services.yml from the app/config/config.yml file
  updated ./app/config/config.yml


  Everything is OK! Now get to work :).

src/Ibw/JobeetBundleがあることを確認!