Laravel 101

Installation

1. install composer ( dependency management ) ref

get composer.phar ( phar = PHP archive ) by

curl -sS https://getcomposer.org/installer | php

To use composer globally,

mv composer.phar /usr/local/bin/composer

2. create new project using laravel in your-project-name directory

composer create-project laravel/laravel your-project-name --prefer-dist

3. test URL : http://localhost/laravel/public/
This launch code from public directory

NOTE : If Error in exception handler,

chmod -R 777 app/storage

4. The folder

/app

__ /controllers

__ /models

__ /views

__ /config

Route
app/routes.php

Route::get('users', function()
{
 return 'Users!';
});

Test URL : http://localhost/laravel/public/index.php/users

‘Users!’ will be echoed.

Route::get('users', 'UserController@getIndex');

Call controller is also possible, UserController.getIndex() is called ref : controller

View

Laravel’s templating system: Blade
that covert to pure PHP

app/views/layout.blade.php

<html>
 <body>
 <h1>Laravel Quickstart</h1>

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

users.blade.php

@extends('layout')

@section('content')
 Users!
@stop

Routes.php

Route::get('users', function()
{
 return View::make('users');
});

Model Migrate Tool

configuration : app/config/database.php

The default db is MySQL. However, we can change to SQLite included in the app/database directory.

using Artisan CLI to gen table

php artisan migrate:make create_users_table

The migration files will be under app/database/migrations

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');
}

Real migrate

php artisan migrate

To rollback, use migrate:rollback command.

Eloquent ORM 

is like ActiveRecord in RoR

app/models/User.php

class User extends Eloquent {}

Route

Route::get('users', function()

{
$users = User::all(); // get all users

return View::make('users')->with('users', $users); // send $users to template
});

Display in the view

@extends('layout')

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

Other things to use

Queues, Unit Testing, iOC container

Static files

will be kept in public folder,

in server.php, there is a logic to check that if the real file really exists, don’t load index.php

if ($uri !== '/' and file_exists($requested))
{
return false;
}

require_once $paths['public'].'/index.php';

 Request Life cycle

1. /server.php
2. /public/index.php
3. /bootstrap/start.php // create Application object ( iOC container : http://laravel.com/docs/ioc )
4. environment detection & setup timezone, error reporting, etc. & register() service providers ( i.e. QueueServiceProvider ) configured for your application. A service provider may register event listeners, view composers, Artisan commands, and more. // http://laravel.com/docs/configuration#environment-configuration
5. app/start files will be loaded.

( application’s start files : register a View composer, configure your logging preferences, set some PHP settings, etc. However, f you feel your start files are getting messy, consider moving some bootstrapping code into service providers.

i.e.

– global.php : automatically included on every request to your application, regardless of environment

i.e. registration of the Logger and the inclusion of your app/filters.php file.

– local.php : only called when the application is executing in the local environment.

NOTE : can create environment configured in your bootstrap/start.php file, i.e. app/start/development.php file, which will be included when any requests enter the application in that environment.

– artisan.php )
6. app/routes.php file will be loaded. Once your routes.php file has been loaded
7. Request object is sent to the application, which returns Response object
8. Response object sent back to client

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s