2015-01-26

Laravel quick guide

1. Install composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
2. Create laravel project
composer create-project laravel/laravel your-project-name --prefer-dist
Apache config
DocumentRoot your-project-location/public
3. Route setting
vi app/routes.php
Route::get('users', function()
{
    return 'Users!';
});
4. Test
http://your-address/index.php/users [ OK ]
http://your-address/users                [ Should be OK ]
5. If not OK, set apache config
<VirtualHost *:80>
  ServerName your-address
  DocumentRoot your-project-location/public

  <Directory "your-project-location/public">
    Require all granted
    AllowOverride all // to allow .htaccess
  </Directory>
</VirtualHost>
6. Create view
cd app/views

vi layout.blade.php
<html>
    <body>
        <h1>Laravel Quickstart</h1>

        @yield('content')
    </body>
</html>

vi users.blade.php
@extends('layout')

@section('content')
    Users!
@stop
7. Set routes
vi app/routes.php
Route::get('users', function()
{
    return View::make('users');
});
8. Set database
vi app/config/database.php
php artisan migrate:make create_users_table
9. Set to make user ORM
cd app/database/migrations
vi *_create_users_table.php
public function up()
{
    Schema::create('users', function($table)
    {
        $table->increments('id');
        $table->string('email')->unique();
        $table->string('name');
        $table->timestamps();
    });
}
public function down()
{
    Schema::drop('users');
}
10. Migrate
php artisan migrate
Check
vi app/models/User.php
class User extends Eloquent {}
11. Set routes
Route::get('users', function()
{
    $users = User::all();

    return View::make('users')->with('users', $users);
});
12. Set view
vi users.blade.php
@extends('layout')

@section('content')
    @foreach($users as $user)
        <p>{{ $user->name }}</p>
    @endforeach
@stop

No comments:

Post a Comment