On Github fprefect / laravel-4-intro-pres
Created by David Jonas / @splitretina
$data = array('name' => 'David'); $rules = array('name' => 'required|between:4,50'); $validator = Validator::make($data, $rules); if ($validator->fails()) { return Redirect::to('page')->withErrors($validator); }
$post = DB::table('posts')->where('slug', '=', 'l4-presentation')->first();
$posts = Post::withTag('laravel')->all();
Global scope, use them from anywhere
function logSignin() { Log::info(sprintf( "User signed in: user_id=%s, remote_ip=%s", Auth::user()->id, Request::server()->getClientIp() )); }
The App facade is a DI container
App::share('KVStore\Redis', function($app) { return new KVStore\Redis($app['config']->get('redis')); }); App::bind('KVStoreInterface', 'KVStore\Redis'); $kvstore = App::make('KVStoreInterface');
Automatically resolves dependencies
$ wget http://getcomposer.org/composer.phar
$ php composer.phar create-project laravel/laravel simple
$ cd simple; vi app/routes.php
Route::get("/hanky", function() { return "Howdy ho!"; });
$ php artisan serve &; open http://localhost:8000/hanky
$ php artisan migrate:make --create --table=guests create_guests_table
class CreateGuestsTable extends Migration { public function up() { Schema::create('guests', function(Blueprint $table) { // You write this part $table->increments('id'); $table->string('name'); $table->string('message'); $table->timestamps(); }); } public funciton down() {} }
$ php artisan migrate
$ vim app/models/Guest.php
class Guest extends \Eloquent { public $fillable = array('name', 'message'); }
$ php artisan controller:make GuestbookController
class GuestbookController extends \BaseController { public function index() { return Guest::all(); } public function show($id) { return (Guest::find($id)) ?: Response::make(null, 404); } public function store() { $guest = Guest::create(Input::only(array('name', 'message'))); return Response::make(null, 201, array('Location' => URL::to('guests', array($guest->id)))); } public function update() { if ( ! $guest = Guest::find($id)) return Response::make(null, 404); $guest->fill(Input::only(array('name', 'message')))->save(); return Response::make(null, 205, array('Location' => URL::to('guests', array($guest->id)))); } public function destroy($id) { if ( ! $guest = Guest::find($id)) return Response::make(null, 404); $guest->delete(); return Response::make(null, 204); } }
Route::resource('guests', 'GuestbookController');
$ php artisan serve
$ curl -i --data "name=David&message=My%20message%20here." \ http://localhost:8000/guests HTTP/1.0 201 Created Location: http://localhost:8000/guests/1 $ curl -q http://localhost:8000/guests [{"id":"1","name":"David","message":"My message here.","created_at":"2013-11-26 17:34:27","updated_at":"2013-11-26 17:34:27"}]