m_shige1979のささやかな抵抗と欲望の日々

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

なんとなく作ったサイト

http://www.it-check-matome.info/


Github(注意すること)

https://github.com/mshige1979

Laravel4のチュートリアル

composerのグローバルインストール

ローカルではちょっとやりにくいのでグローバルインストールする

手順
[root@localhost ~]# curl -sS https://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /root/composer.phar
Use it: php composer.phar
[root@localhost ~]# mv composer.phar /usr/local/bin/composer
[root@localhost ~]# which composer
/usr/local/bin/composer
[root@localhost ~]#

Laravel4のインストール

指定のディレクトリに移動して、「TestProject」へ作成
[root@localhost ~]# mkdir -p /var/www/html/dev.example.com
[root@localhost ~]# cd /var/www/html/dev.example.com
[root@localhost dev.example.com]# composer create-project laravel/laravel TestProject
Installing laravel/laravel (v4.1.18)
  - Installing laravel/laravel (v4.1.18)
    Loading from cache

Created project in TestProject
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing symfony/translation (v2.4.2)
    Loading from cache

  - Installing symfony/routing (v2.4.2)
    Loading from cache

  - Installing symfony/process (v2.4.2)
    Loading from cache

  - Installing psr/log (1.0.0)
    Loading from cache

  - Installing symfony/debug (v2.4.2)
    Loading from cache

  - Installing symfony/http-foundation (v2.4.2)
    Loading from cache

  - Installing symfony/event-dispatcher (v2.4.2)
    Loading from cache

  - Installing symfony/http-kernel (v2.4.2)
    Loading from cache

  - Installing symfony/finder (v2.4.2)
    Loading from cache

  - Installing symfony/dom-crawler (v2.4.2)
    Loading from cache

  - Installing symfony/css-selector (v2.4.2)
    Loading from cache

  - Installing symfony/console (v2.4.2)
    Loading from cache

  - Installing symfony/browser-kit (v2.4.2)
    Loading from cache

  - Installing swiftmailer/swiftmailer (v5.0.3)
    Loading from cache

  - Installing stack/builder (v1.0.1)
    Loading from cache

  - Installing predis/predis (v0.8.5)
    Loading from cache

  - Installing phpseclib/phpseclib (0.3.6)
    Loading from cache

  - Installing patchwork/utf8 (v1.1.20)
    Loading from cache

  - Installing nesbot/carbon (1.8.0)
    Loading from cache

  - Installing monolog/monolog (1.7.0)
    Loading from cache

  - Installing nikic/php-parser (v0.9.4)
    Loading from cache

  - Installing jeremeamia/superclosure (1.0.1)
    Loading from cache

  - Installing filp/whoops (1.0.10)
    Loading from cache

  - Installing ircmaxell/password-compat (1.0.3)
    Loading from cache

  - Installing d11wtq/boris (v1.0.8)
    Loading from cache

  - Installing symfony/filesystem (v2.4.2)
    Loading from cache

  - Installing classpreloader/classpreloader (1.0.1)
    Loading from cache

  - Installing laravel/framework (v4.1.23)
    Loading from cache

symfony/translation suggests installing symfony/config ()
symfony/translation suggests installing symfony/yaml ()
symfony/routing suggests installing symfony/config (For using the all-in-one router or any loader)
symfony/routing suggests installing symfony/yaml (For using the YAML loader)
symfony/routing suggests installing symfony/expression-language (For using expression matching)
symfony/routing suggests installing doctrine/annotations (For using the annotation loader)
symfony/event-dispatcher suggests installing symfony/dependency-injection ()
symfony/http-kernel suggests installing symfony/class-loader ()
symfony/http-kernel suggests installing symfony/config ()
symfony/http-kernel suggests installing symfony/dependency-injection ()
predis/predis suggests installing ext-phpiredis (Allows faster serialization and deserialization of the Redis protocol)
phpseclib/phpseclib suggests installing pear-pear/PHP_Compat (Install PHP_Compat to get phpseclib working on PHP < 4.3.3.)
monolog/monolog suggests installing mlehner/gelf-php (Allow sending log messages to a GrayLog2 server)
monolog/monolog suggests installing raven/raven (Allow sending log messages to a Sentry server)
monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
monolog/monolog suggests installing ruflin/elastica (Allow sending log messages to an Elastic Search server)
monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
monolog/monolog suggests installing aws/aws-sdk-php (Allow sending log messages to AWS services like DynamoDB)
laravel/framework suggests installing doctrine/dbal (Allow renaming columns and dropping SQLite columns.)
Writing lock file
Generating autoload files
Generating optimized class loader
Application key [CKu2LWRkwDQZYGGq2K8A5d7PZOGD4lix] set successfully.
[root@localhost dev.example.com]#
TestProject配下へ移動して確認
[root@localhost dev.example.com]# cd TestProject/
[root@localhost TestProject]# ll
合計 96
-rw-r--r--  1 root root   145  1月 19 10:14 2014 CONTRIBUTING.md
drwxr-xr-x 12 root root  4096  1月 19 10:14 2014 app
-rwxr-xr-x  1 root root  2451  1月 19 10:14 2014 artisan
drwxr-xr-x  2 root root  4096  1月 19 10:14 2014 bootstrap
-rw-r--r--  1 root root   697  1月 19 10:14 2014 composer.json
-rw-r--r--  1 root root 56274  3月  5 23:55 2014 composer.lock
-rw-r--r--  1 root root   566  1月 19 10:14 2014 phpunit.xml
drwxr-xr-x  3 root root  4096  1月 19 10:14 2014 public
-rw-r--r--  1 root root  1795  1月 19 10:14 2014 readme.md
-rw-r--r--  1 root root   519  1月 19 10:14 2014 server.php
drwxr-xr-x 20 root root  4096  3月  5 23:55 2014 vendor
[root@localhost TestProject]#

設定

composerのアップデート
[root@localhost TestProject]# composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
Generating optimized class loader
[root@localhost TestProject]#
設定ファイルの編集
[root@localhost TestProject]# vim app/config/app.php
----
      /*
      |--------------------------------------------------------------------------
      | Application Timezone
      |--------------------------------------------------------------------------
      |
      | Here you may specify the default timezone for your application, which
      | will be used by the PHP date and date-time functions. We have gone
      | ahead and set this to a sensible default for you out of the box.
      |
      */
 
      'timezone' => 'Asia/Tokyo',
 
      /*
      |--------------------------------------------------------------------------
      | Application Locale Configuration
      |--------------------------------------------------------------------------
      |
      | The application locale determines the default locale that will be used
      | by the translation service provider. You are free to set this value
      | to any of the locales which will be supported by the application.
      |
      */
 
      'locale' => 'ja',
----
データベースを作成
[root@localhost TestProject]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.6.14-log Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database laravel_db;
Query OK, 1 row affected (7.06 sec)

mysql> quit;
Bye
[root@localhost TestProject]#

データベースを使用するので「laravel_db」というデータベースを作成する

データベースの設定を編集
[root@localhost TestProject]# vim app/config/database.php
----
<?php

return array(

    'fetch' => PDO::FETCH_CLASS,

    'default' => 'mysql',

    'connections' => array(

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

        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'laravel_db',
            'username'  => 'root',
            'password'  => 'password',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'unix_socket' => '/tmp/mysql.sock',
        ),
----

MySQLの場合、「unix_socket」を指定する場合は必要な場合もある。

ジェネレータプラグインを導入

composer.json
[root@localhost TestProject]# vim composer.json
----
    "require": {
        "laravel/framework": "4.1.*",
        "way/generators": "1.1"
    },
----

※バージョンは「1.1」を指定

composer updateを実行
[root@localhost TestProject]# composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing mustache/mustache (v2.4.1)
    Downloading: 100%

  - Installing way/generators (1.1)
    Downloading: 100%

Writing lock file
Generating autoload files
Generating optimized class loader
[root@localhost TestProject]#
プラグインを追加
[root@localhost TestProject]# vim app/config/app.php
----
'providers' => array(

        'Illuminate\Foundation\Providers\ArtisanServiceProvider',
        'Illuminate\Auth\AuthServiceProvider',
        'Illuminate\Cache\CacheServiceProvider',
        'Illuminate\Session\CommandsServiceProvider',
        'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider',
        'Illuminate\Routing\ControllerServiceProvider',
        'Illuminate\Cookie\CookieServiceProvider',
        'Illuminate\Database\DatabaseServiceProvider',
        'Illuminate\Encryption\EncryptionServiceProvider',
        'Illuminate\Filesystem\FilesystemServiceProvider',
        'Illuminate\Hashing\HashServiceProvider',
        'Illuminate\Html\HtmlServiceProvider',
        'Illuminate\Log\LogServiceProvider',
        'Illuminate\Mail\MailServiceProvider',
        'Illuminate\Database\MigrationServiceProvider',
        'Illuminate\Pagination\PaginationServiceProvider',
        'Illuminate\Queue\QueueServiceProvider',
        'Illuminate\Redis\RedisServiceProvider',
        'Illuminate\Remote\RemoteServiceProvider',
        'Illuminate\Auth\Reminders\ReminderServiceProvider',
        'Illuminate\Database\SeedServiceProvider',
        'Illuminate\Session\SessionServiceProvider',
        'Illuminate\Translation\TranslationServiceProvider',
        'Illuminate\Validation\ValidationServiceProvider',
        'Illuminate\View\ViewServiceProvider',
        'Illuminate\Workbench\WorkbenchServiceProvider',

        'Way\Generators\GeneratorsServiceProvider'
    ),
----

※providersの中に追加

コマンドを確認
root@localhost TestProject]# php artisan
Laravel Framework version 4.1.23

・
・
・

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.

・
・
・
generate:scaffoldで作成
[root@localhost TestProject]# php artisan generate:scaffold post --fields="title:string, body:text"
Created /var/www/html/dev.example.com/TestProject/app/models/Post.php
Created /var/www/html/dev.example.com/TestProject/app/controllers/PostsController.php
Created /var/www/html/dev.example.com/TestProject/app/views/posts/index.blade.php
Created /var/www/html/dev.example.com/TestProject/app/views/posts/show.blade.php
Created /var/www/html/dev.example.com/TestProject/app/views/posts/create.blade.php
Created /var/www/html/dev.example.com/TestProject/app/views/posts/edit.blade.php
Created /var/www/html/dev.example.com/TestProject/app/views/layouts/scaffold.blade.php
Generating optimized class loader
Created /var/www/html/dev.example.com/TestProject/app/database/migrations/Create_posts_table.php
Created /var/www/html/dev.example.com/TestProject/app/database/seeds/PostsTableSeeder.php
Updated /var/www/html/dev.example.com/TestProject/app/database/seeds/DatabaseSeeder.php
Created /var/www/html/dev.example.com/TestProject/app/tests/controllers/PostsTest.php
Updated /var/www/html/dev.example.com/TestProject/app/routes.php
[root@localhost TestProject]#
マイグレーション
[root@localhost TestProject]# php artisan migrate
Migration table created successfully.
Migrated: 2014_03_06_004514_create_posts_table
[root@localhost TestProject]#
簡易起動を行う
[root@localhost TestProject]# php artisan serve --host=192.168.51.129
Laravel development server started on http://192.168.51.129:8000

画面

http://hostname:8000/postでアクセス

f:id:m_shige1979:20140308084040j:plain

新規追加

f:id:m_shige1979:20140308084044j:plain

一覧

f:id:m_shige1979:20140308084049j:plain

まとめ

最近のPHPではこれが人気出そうな感じがあったのでインストール。
composerでインストールできて簡単な感じがした。
いくつかのframeworkは理解してきちんと使い分けができるようにする必要がありそう。
このframeworkの特性を理解しないと…

広告を非表示にする