Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to Laravel

Intro to Laravel

Introduction to Laravel for veteran PHP framework.

Rahmat Awaludin

March 18, 2014
Tweet

More Decks by Rahmat Awaludin

Other Decks in Programming

Transcript

  1. –Laravel Community “… we’ve attempted to combine the very best

    of what we have seen in other web frameworks, including frameworks implemented in other languages, such as Ruby on Rails, ASP.NET MVC and Sinatra.”
  2. Request Lifecycle Request Route Controller Model View Start Files
 !

    Events
 ! Filters http://laravel.com/docs/lifecycle
  3. The MVC Layers - Model • Powerfull Eloquent ORM •

    Native support for MySQL, SQL Server, PostgreSQL, SQLite
 
 
 
 file: app/model/project.php
 class Project extends Eloquent {} • Active record
 $allproject = Project::all();
 $healthyproject = Project::find(2);
 $flu = Project::where(‘name’,’Fighting Flu’);
  4. The MVC Layers - CRUD CREATE
 $project = new Project;


    $project->title = ‘Group gathering’;
 $project->save(); READ
 echo $project->title;
 echo $project->manager->email;
 
 UPDATE
 $project = Project::find(10);
 $project->description = “Awesome project”;
 $project->save();
 
 DELETE
 $user->delete();
  5. The MVC Layers - Relationships • One to One •

    One to Many • Many to Many • Polymorphic Relations
  6. The MVC Layers - Relationships ! ! ! • File

    app/model/manager.php
 class Manager extends Eloquent {
 public function projects() {
 return $this->hasMany(‘Project’);
 }
 } • File app/model/project.php
 class Manager extends Eloquent {
 public function manager() {
 return $this->belongsTo(‘Manager’);
 }
 }
  7. The MVC Layers - Accessing Relationship $manager = Manager::find(1)
 foreach

    ( $manager->projects as $project) {
 . . . .
 } $project = Project::find(2);
 echo $project->manager->first_name;
 
 $project->manager->email = “[email protected]”;
 $project->manager->save();
  8. The MVC Layers - Support Soft Delete // in app/model/project.php


    protected $softDelete = true; // Automatically excludes soft delete rows
 Project::all(); // Include soft deleted rows
 Project::withTrashed()->get(); // Include only soft deleted rows
 Project::onlyTrashed()->get(); // Undo soft delete
 $project = Project::withTrashed()->where(‘id’,’=‘,’1’);
 $project->restore();
 
 // Skip soft delete
 $project->forceDelete();
  9. The MVC Layers - Support Query Scope // file app/model/movie.php


    class Movie extends Eloquent {
 public function scopePopular() {
 return $query->where(‘rating’,’>’,’5’);
 }
 }
 
 Movie::popular()->get();
 
 // Dynamic scope
 class User extends Eloquent {
 public function scopeOfType($query, $type)
 {
 return $query->whereType($type);
 }
 }
 
 $users = User::ofType('member')->get();
  10. The MVC Layers - Support Accessor & Mutators // Accessor


    public function getFullnameAttribute($value)
 {
 return $this->first_name . ‘ ‘ $this->last_name;
 } // fullname attribute become available
 echo $manager->fullname; // Mutator
 public function setFirstnameAttribute($value)
 {
 $this->attributes[‘name’] = strtolower($value);
 }
  11. The MVC Layers - Support Eager Loading • Bad ways,

    many queries.
 foreach (Project::all() as $project)
 {
 echo $project->manager->first_name;
 }
 
 // become this
 select * from projects;
 select * from manager where id = 1;
 select * from manager where id = 2;
 select * from manager where id = 3;
 select * from manager where id = 4;
 . . . . 
 . . . . • Good ways, least queries.
 foreach (Project::with(‘manager’)->get() as $project)
 {
 echo $project->manager->first_name;
 }
 
 // become this
 select * from projects
 select * from manager where id in (1, 2, 3, 4, 5, …)
  12. The MVC Layers - Other Cool Stuff • Model Events

    • Converting to Array/JSON • etc..
 
 http://laravel.com/docs/eloquent
  13. The MVC Layers - Migrations & Seeding • Version control

    for your database structure (and content) • Paired with Schema Builder (manipulate db structure) • Run with Artisan
 
 http://laravel.com/docs/schema
 http://laravel.com/docs/migrations
  14. The MVC Layers - Migrations // Generate migrations
 $ php

    artisan migrate:make create_posts_table // Setup migration schema (we also can setup it automatically)
 <?php
 use Illuminate\Database\Migrations\Migration;
 use Illuminate\Database\Schema\Blueprint;
 
 class CreatePostsTable extends Migration {
 /**
 * Run the migrations.
 *
 * @return void
 */
 
 public function up()
 {
 Schema::create('posts', function(Blueprint $table) {
 $table->increments('id');
 $table->string(‘title’)->nullable();
 $table->text('body');
 $table->timestamps();
 $table->softDeletes();
 });
 }
 
 /**
 * Reverse the migrations.
 *
 * @return void
 */
 
 public function down()
 {
 Schema::drop('posts');
 }
 }
  15. The MVC Layers - Migrations // Run migrations
 $ php

    artisan migrate
 
 // Results ! ! // Reverse migration
 $ php artisan migrate:rollback
 
 // Results
 Posibble migration: - modify table - modify field - modify index/ key - modify storage engines
  16. The MVC Layers - Blade Engine • Stock templating engine

    for Laravel • Complex logic. • Supports template inheritance and sections
 <html>
 <head>
 <title>@yield(‘page_title’)</title>
 @yield(‘css’)
 @yield(‘javascript’)
 </head>
 
 <body>
 Some text
 @yield(‘content’)
 </body>
 </html>

  17. The MVC Layers - Blade Engine @extends(‘layout’) @section(‘page_title’, ‘Login Page’);

    @section(‘css’)
 <link rel=“stylesheet” type=“text/css” href=“mystyle.css”/>
 @endsection @section(‘javascript’)
 <script type=‘text/javascript’ src=“app.js”></script>
 @endsection @section(‘content’)
 This is the content for a particular page
 @endsection
  18. The MVC Layers - Blade Engine • Some control structures


    @if (…)
 …
 @elseif …
 @else …
 @endif
 
 @unless (…)
 ….
 @endunless
 
 
 @for (…)
 ….
 @endfor
 
 @while (…)
 ….
 @endwhile
 
 @foreach( …)
 ….
 @endforeach
  19. The MVC Layers - Blade Engine • Alternate ways to

    echo variables
 My name is <?php echo $user->name; ?>, nice to meet you!
 My name is {{ $user->name }}, nice to meet you!
  20. Routing • GET • POST • PUT • DELETE •

    ANY • filters (auth)
 
 http://laravel.com/docs/routing
  21. The MVC Layers - Controller • Basic controller 
 //

    The controller itself
 class AuthController extends BaseController
 {
 public function showLoginForm() {
 return View::make(‘auth.loginform’);
 } 
 }
 
 // Routing file
 Route::get(‘/auth’, ‘AuthController@showLoginForm’);
  22. The MVC Layers - Controller • RESTful controller 
 //

    The controller itself
 class ProjectController extends BaseController{
 {
 public function getShowAll()
 {
 // . . . . . . 
 }
 
 public function postAdd()
 {
 // . . . . . .
 }
 }
 
 // Routing file
 Route::controller(‘/projects’,’ProjectController’
  23. The MVC Layers - Controller • Resource controller • Generated

    using Artisan CLI • Allow easy RESTful implementation • Paths and route names are generated automatically

  24. Composer • Dependency management for modern PHP development • It

    allows you to declare the dependent libraries your project needs and it will install them in your project for you • Reusable component • Open/private source • > 5000 packages • Well tested & enterprise ready 
 (some, actually :D) • http://getcomposer.org • http://packagist.org
  25. Packages Development • Integrate with core app seamlessly • Stand-alone

    : • Routes • Controller • Model • Migration • View • Package dependency ! http://laravel.com/docs/packages
  26. Creating package // configure identity
 $ vim app/config/workbench.php
 
 //

    Issuing The Workbench Artisan Command
 $ php artisan workbench elplus/modul-career —resources 

  27. Creating package // Package structure // Create route, model,
 migration,

    view, 
 controller as usual.
 But, in workbench.
 
 // Register service 
 provider to 
 app/config/app.php
 
 // Run migration
 (if needed) // Publish Asset 
 (if needed)
 // Publish Package to 
 Composer repository 
 (if needed)

  28. Things we don’t cover • Error logging • Localization •

    Testing • Facade • IoC • Dependency Injection • Artisan command • Design Pattern used in Laravel • and more…
 http://laravel.com/docs
  29. – Rahmat Awaludin “Laravel is powerful and elegant while still

    being easy to learn and use. Yeah, thats what a framework should do!”