laravel4を使って最速簡易ブログ作成の方法

laravel4を使って簡易ブログを作るメモ。
PHP5.4以上+laravel4+composer+laravel4-generatorを使用して一時期のrailsみたいな最速ブログ生成を目指します。

## 前提条件
- PHP5.4以上
- mysql5.x なければapp内のsqliteを使用する
- composer

##プロジェクト作成
まずはLaravel4をインストール

```
$composer create-project laravel/laravel  project名 --prefer-dist
```

##laravel4-generatorを導入
先ほど作成したプロジェクトに入り、composer.jsonに以下を追記。

```json:composer.json
"require": {
    "laravel/framework": "4.0.*",
    "way/generators": "dev-master"
},
"minimum-stability" : "dev"//追記
```

composerでインストール。

```
$composer update
```

ServiceProviderに登録。（laravel4-genaratorを使えるようにする）

```php:app/config/app.php
'Way\Generators\GeneratorsServiceProvider'
```

php artisanで追加されたコマンドが確認できる

```
$php artisan
Laravel Framework version 4.0.6

Usage:
  [options] command [arguments]

Options:
  --help           -h Display this help message.
  --quiet          -q Do not output any message.
  --verbose        -v|vv|vvv Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
  --version        -V Display this application version.
  --ansi              Force ANSI output.
  --no-ansi           Disable ANSI output.
  --no-interaction -n Do not ask any interactive question.
  --env               The environment the command should run under.

Available commands:
  changes               Display the framework change list
  clear-compiled        Remove the compiled class file
  down                  Put the application into maintenance mode
  dump-autoload         Regenerate framework autoload files
  help                  Displays help for a command
  list                  Lists commands
  migrate               Run the database migrations
  optimize              Optimize the framework for better performance
  routes                List all registered routes
  serve                 Serve the application on the PHP development server
  tinker                Interact with your application
  up                    Bring the application out of maintenance mode
  workbench             Create a new package workbench
asset
  asset:publish         Publish a package's assets to the public directory
auth
  auth:reminders        Create a migration for the password reminders table
cache
  cache:clear           Flush the application cache
command
  command:make          Create a new Artisan command
config
  config:publish        Publish a package's configuration to the application
controller
  controller:make       Create a new resourceful controller
db
  db:seed               Seed the database with records
generate //ここが追加されたgenerateコマンド
  generate:controller   Generate a new controller.
  generate:form         Dump form HTML for a model
  generate:migration    Generate a new migration.
  generate:model        Generate a new model.
  generate:pivot        Generate a pivot table
  generate:resource     Generate a resource.
  generate:scaffold     Generate scaffolding for a resource.
  generate:seed         Generate a seed file.
  generate:test         Generate a PHPUnit test class.
  generate:view         Generate a new view.
key
  key:generate          Set the application key
migrate
  migrate:install       Create the migration repository
  migrate:make          Create a new migration file
  migrate:refresh       Reset and re-run all migrations
  migrate:reset         Rollback all database migrations
  migrate:rollback      Rollback the last database migration
queue
  queue:listen          Listen to a given queue
  queue:subscribe       Subscribe a URL to an Iron.io push queue
  queue:work            Process the next job on a queue
session
  session:table         Create a migration for the session database table
```

## scaffoldingで簡易ブログ生成
railsではおなじみのscaffoldingで簡易ブログを生成します。

```
$php artisan generate:scaffold post --fields="title:string, body:text"
Created /app/fast-blog/app/models/Post.php
Created /app/fast-blog/app/controllers/PostsController.php
Created /app/fast-blog/app/views/posts/index.blade.php
Created /app/fast-blog/app/views/posts/show.blade.php
Created /app/fast-blog/app/views/posts/create.blade.php
Created /app/fast-blog/app/views/posts/edit.blade.php
Created /app/fast-blog/app/views/layouts/scaffold.blade.php
Generating optimized class loader
Created /app/fast-blog/app/database/migrations/Create_posts_table.php
Created /app/fast-blog/app/database/seeds/PostsTableSeeder.php
Updated /app/fast-blog/app/database/seeds/DatabaseSeeder.php
Created /app/fast-blog/app/tests/controllers/PostsTest.php
Updated /app/fast-blog/app/routes.php
```

次に、databaseの設定をします。デフォルトがmysqlになっているので環境にあわせて各自
変更してください。もしsqliteがつかえるなら、Defalut設定をmysqlからsqliteに変更すればOK。
以下はmysqlの場合。

```php:app/config/database.php
(中略)
       /*
        |--------------------------------------------------------------------------
        | Database Connections
        |--------------------------------------------------------------------------
        |
        | Here are each of the database connections setup for your application.
        | Of course, examples of configuring each database platform that is
        | supported by Laravel is shown below to make development simple.
        |
        |
        | All database work in Laravel is done through the PHP PDO facilities
        | so make sure you have the driver for your particular database of
        | choice installed on your machine before you begin development.
        |
        */

        'connections' => array(

                'sqlite' => array(
                        'driver'   => 'sqlite',
                        'database' => __DIR__.'/../database/production.sqlite',
                        'prefix'   => '',
                ),

                'mysql' => array(
                        'driver'    => 'mysql',
                        'host'      => '127.0.0.1',
                        'database'  => 'DB名',
                        'username'  => 'root',
                        'password'  => '',
                        'charset'   => 'utf8',
                        'collation' => 'utf8_unicode_ci',
                        'prefix'    => '',
                ),
(中略)
```

あとは、テーブルを作成。

```
$php artisan migrate
Migrated: 2013_08_27_152716_create_posts_table
```

これで簡易ブログの完成です。
PHP5.4以上をお使いならば、以下でビルトインサーバーをたてられます。

```
$php artisan serve
```
あとはアクセスするのみ！

http://localhost:8000/posts

blog一覧
![スクリーンショット 2013-08-28 1.10.46.png](https://qiita-image-store.s3.amazonaws.com/0/13609/57f75c4b-c8af-797e-5137-b594da58a6fd.png)


作成画面
![スクリーンショット 2013-08-28 1.10.58.png](https://qiita-image-store.s3.amazonaws.com/0/13609/9e557d8d-c2d2-c5f0-b0ea-0cf32fa11816.png)


Laravel4楽しい！簡単！おいしい！
Let's enjoy Laravel4!!!!


＊もし権限周りで動かない場合は、以下を実行してください。

```
$chmod -R 777 app/storage
```