サイトアイコン マーカーネット株式会社

Laravel5を操作してみました

先月リリースされました、PHPのフレームワークLaravel5を操作してみました。

Contents

1. プロジェクトの作成

インストールするディレクトリに移動して「test」という名前のプロジェクトを作成します。

composer create-project laravel/laravel test –prefer-dist

2. パーミッションの変更

プロジェクトのディレクトリ(test)直下の
storageディレクトリ以下のパーミッションを777に変更します。

chmod -R 777 storage

3. コントローラの作成

artisanコマンドを利用してTestという名前でコントローラを作成します。

cd test/
php artisan make:controller TestController

app/Http/Controllers/TestController.php
にコントローラのファイルが作成されます。

4. ルーティング設定

「/」でアクセスした場合、Testコントローラのindexを呼ぶよう、
app/Http/routes.phpを編集します。

Route::get(‘/’, ‘TestController@index’);

5. viewの編集

各ページ共通のレイアウトファイル「resources/views/app.blade.php」を
編集します。

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
</head>
<body>
@yield(‘content’)
</body>
</html>

@yield(‘content’)が個別のviewを表示する部分です。

次に「resources/views」に「test」フォルダを作成して、
index.blade.phpという「TestController@index」用の個別のviewファイルを作成します。

@extends(‘app’)@section(‘content’)
<h1>Laravel5 テスト</h1>
@endsection

@extends(‘app’) でapp.blade.phpをレイアウトファイルとして指定して、
@section(‘content’)

@endsection
部分を表示します。

6. Testコントローラのindex()に5.で作成したviewを指定します。

class TestController extends Controller {/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
return view(‘test.index’);
}

以上で、
ブラウザでルート(デフォルトでhttp://○○○○/test/public/)を表示すると
Laravel5 テスト
と表示されるようになります。

モバイルバージョンを終了