On Github Pegasuz / laravel-talk
An introduction to the Laravel Framework
PHP Developer - Studio Emma
@pegasuz_
It went from version 1 to version 4.2 in 3 years.
Laravel 1: june 2011 Laravel 2: november 2011 Laravel 3: february 2012 Laravel 4: May 2013 Laravel 4.2: May 2014 Laravel 5 (4.3) November 2014Installs in the last 30 days
Zend Framework 2 70 000 Symfony 2 38 000 Laravel 4 76 000composer create-project laravel/laravel my-laravel-app
composer global require "laravel/installer=~1.1"
laravel new my-laravel-app
Setup a vagrant virtual machine using homestead (or phansible.com, or puphpet.com)
http://laravel.com/docs/homestead Ubuntu 14.04 PHP 5.6 HHVM Nginx MySQL Postgres Node (With Bower, Grunt, and Gulp) Redis Memcached Beanstalkd Laravel Envoy Fabric + HipChat ExtensionConfig::get('filename.configKey');
php artisan
app/routes.php
Route::get('/', function() { return 'Hello World'; });
Route::post('/', function() { return 'Show this on POST'; });
Route::any('/', function() { return 'Hello World'; });
Route::get('/post/{id}', function($id) { return 'Show post ' . $id; });
Route::get('/post/{id?}', function($id = null) { return 'Show post ' . $id; });
Route::get('/post/{id?}', function($id=123) { return 'Show post ' . $id; });
Route::get('/post/{id}', function($id) { return 'Show post ' . $id; })->where('id', '[0-9]+');
Route::get('/', ['as' => 'home', function() { return 'Hello World!'; }]);
$url = URL::route('home');
Limit access to a route using build-in or custom filters. Place them in app/filters.php
Route::get('/', ['before' => 'auth', function() { return 'Hello Authenticated User!'; }]);
Point a route to a specific action in a specific controller
Route::get('post/{id}', 'PostController@show');
Route::get('post/{id}', 'Namespace\PostController@show');
Route::get('home', ['uses' => 'HomeController@index', 'as' => 'home']);
$url = URL::action('HomeController@index');
Use (only) restfully routes on a controller
Route::resource('post', 'PostController');Verb Path Action Route Name GET /resource index resource.index GET /resource/create create resource.create POST /resource store resource.store GET /resource/{resource} show resource.show GET /resource/{resource}/edit edit resource.edit PUT/PATCH /resource/{resource} update resource.update DELETE /resource/{resource} destroy resource.destroy
Standard php (.php) or Blade template engine (.blade.php). Found in app/views
<p>Hello, {{{ $name }}}.</p> @if($name) { <p>Hello, {{{ $name }}}</p> @else <p>Hello guest</p> @endif <ul> @foreach($names as $name) <li>{{{ $name }}}</li> @endforeach </ul>
Route::get('/', function() { return View::make('home.index', ['name' => 'Pieter']); });
<!--app/views/layouts/master.blade.php--> <html> <body> @yield('content') </body> </html>
<!--app/views/home/index.blade.php--> @extends('layouts.master') @section('content') <p>This is my body content.</p> @stop
Includes (partials) are exterior aware.
@include('view.name')
@include('view.name', array('some'=>'data'))
Define your database structure and log changes.
php artisan migrate:make migrationName
class CreateRestaurantsTable extends Migration { public function up() { Schema::create('Restaurants', function(Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('address'); $table->timestamps(); }); } public function down() { Schema::drop('Restaurants'); } }
Populate the database with (dummy) records.
php artisan db:seed
Perform queries on a database
$users = DB::table('users')->get();
$user = DB::table('users')->where('name', 'Pieter')->first();
$users = DB::table('users') ->where('stars', '>', '15') ->orderBy('name', 'desc') ->get();
DB::table('users')->insert( ['email' => 'pieter@studioemma.com', 'name' => 'Pieter'] );
DB::table('users') ->where('id', 1) ->update(['name' => 'Pieter Meyvaert']);
DB::table('users')->where('stars', '<', 100)->delete();
Laravel's implementation of a Active Record ORM system. Designed for a quick start, using some ground rules (which can be overridden). Create models in app/models.
class User extends Eloquent {}
$users = User::all();
$users = User::find(123);
$users = User::where('name', 'LIKE', 'Pieter')->get();
$users = User::where('stars', '>', '10')->latest()->take(5)->get();
$user = new User; $user->name = 'Pieter'; $user->save();
$user = User::find(123); $user->email = 'pieter@studioemma.com'; $user->save();
$user = User::find(123); $user->delete();
User::destroy(123);
On delete, mark database record of deletion, but do not actual delete it. Do not show these records in any queries.
class User extends Eloquent { use SoftDeletingTrait; protected $dates = ['deleted_at']; }
$table->softDeletes();
$users = User::withTrashed()->where('name', 'LIKE', 'Pieter')->get();
$user->restore();
It can be hard to find the one you belong to.
Especially if there are multiple...
class User extends Eloquent { public function phone() { return $this->hasOne('Phone'); } } class Phone extends Eloquent { public function user() { return $this->belongsTo('User'); } }
$phone = User::find(123)->phone;
$user = Phone::find(321)->user;
class Talk extends Eloquent { public function comments() { return $this->hasMany('Comment'); } } class Comment extends Eloquent { public function talk() { return $this->belongsTo('Talk'); } }
$comments = Talk::find(123)->comments;
$comments = Talk::find(123) ->comments() ->where('title', 'LIKE', 'great') ->first();
$talk = Comment::find(321)->talk();
class User extends Eloquent { public function talks() { return $this->belongsToMany('Talk'); } } class Talk extends Eloquent { public function users() { return $this->belongsToMany('User'); } }
$talks = User::find(123)->talks
$users = Talk::find(321)->users
php artisan generate:pivot users talks
Can be found in app/controllers. They all extend BaseController
class UserController extends BaseController { public function showProfile($id) { $user = User::find($id); return View::make('user.profile', ['user' => $user]); } }
Forms are defined in the views
{{ Form::open(['action' => 'PostController@save')] }} {{ Form::label('name', 'Your name') }} {{ Form::text('name') }} {{ Form::label('language','Your favorite language') }} {{ Form::select('language', ['php', 'asp'], 'php') }} {{ Form::submit('Submit!') }} {{ Form::close() }}
Automagically fill the form based on a given model
{{ Form::model($user, ['action' => 'PostController@save')] }} {{ Form::label('name', 'Your name') }} {{ Form::text('name') }} {{ Form::submit('Submit!') }} {{ Form::close() }}
$name = Input::get('name');
$name = Input::get('name', 'Pieter');
$input = Input::all();
Input::has('name')
Build-in authentication, uses the User model. No RBAC!
if (Auth::attempt(['email' => $email, 'password' => $password])) { // Authenticated }
Auth::check()
$user = Auth::user()
Auth::logout();
Route::get('/', ['before' => 'auth', function() { return 'Hello Authenticated User!'; }]);
It all makes sense now!
// Create migration php artisan auth:reminders-table // Run the migration php artisan migrate // Generate a controller for this php artisan auth:reminders-controller // Create a view ( reminders/password.remind.blade.php ) {{ Form::open(['action'=>'RemindersController@postRemind']) }} {{ Form::label('email') }} {{ Form::test('email') }} {{ Form::submit('Submit!') }} {{ Form:close() }}
Mail::send('emails.welcome', ['key' => 'value'], function($message) { $message->to('foo@example.com', 'John Smith')->subject('Welcome!'); });
// app/start/global.php Log::useDailyFiles(storage_path().'/logs/logfile.log');
Queue::push('SendEmail', array('message' => $message)); class SendEmail { public function fire($job, $data) {} }
Event::listen('auth.login', function($user) { $user->last_login = new DateTime; $user->save(); });
@servers(['web' => '192.168.1.1']) @task('deploy', ['on' => 'web']) cd /var/www git pull origin master @endtask
envoy run deploy
composer require barryvdh/laravel-ide-helper
https://joind.in/11933