Updated: 2017-04-13 18:52:38 CST +08

Laravel Update 4.2 to 5.0

Basic Flow

/public/index.php->Route:
Route->Controller:
Controller->Model:
Model-->Controller:data
Controller->View:

Env

Laravel 5 doesn’t have environment detection, use .env.example

4.2 5
env app/config/[envfolder] .env

Namespace

  • Naming Your Application (optional)

    php artisan app:name Mstar
    

Route

4.2 5
middleware filter middleware
where(global) app/route.php app/Providers/RouteServiceProvider.php

Middleware

CSRF Protection is default middleware in Laravel 5

  • 4.2

    // app/filter.php
    Route::filter('csrf', function()
    {
        if (Session::token() != Input::get('_token'))
        {
            throw new IlluminateSessionTokenMismatchException;
        }
    });
    
  • 5

    // app/Http/Middleware/VerifyCsrfToken.php
    class VerifyCsrfToken extends BaseVerifier {
    
        public function handle($request, Closure $next)
        {
            return parent::handle($request, $next);
        }
    }
    

Register

// app/Http/Kernel.php

// The application's global HTTP middleware stack.
protected $middleware = [
    //...
    'AppHttpMiddlewareVerifyCsrfToken',
];

// The application's route middleware.
protected $routeMiddleware = [
    //...
    'csrf' => 'AppHttpMiddlewareVerifyCsrfToken',
];

Using

  • 4.2

    Route::get('/', array('before' => 'csrf|auth', 'uses' => 'index@HomeController'));
    
  • 5

    Route::get('/', ['middleware' => ['csrf', 'auth'], 'uses' => 'index@HomeController']);
    

Where

Global

  • 4.2

    // app/route.php
    Route::pattern('id', '[0-9]+');
    
  • 5

    // app/Providers/RouteServiceProvider.php
    public function boot(Router $router)
    {
        $router->pattern('id', '[0-9]+');
        parent::boot($router);
    }
    

Controller

Parent Class & Namespace

  • 4.2

    // app/controllers/HomeController.php
    
    class HomeController extends BaseController {}
    
  • 5

    // app/Http/Controllers/HomeController.php
    
    namespace AppHttpControllers;
    
    class HomeController extends Controller {}
    

Model

Parent Class & Namespace

  • 4.2

    // app/models/User.php
    class User extends Eloquent {
    }
    
  • 5

    // app/User.php
    use IlluminateDatabaseEloquentModel;
    
    class User extends Model {
    }
    

Others

  • Laravel 5: if you wanna create instance, you have to set

    protected $guarded = [];
    
    //default
    protected $guarded = ['*'];
    
  • Relation has to setting namespace

    public function users()
    {
        return $this->hasMany('AppModelsUser');
    }
    

Eager Loading

  • 4.2

    $report->report_assigns
    
  • 5

    $report->reportAssigns
    

View

Echo

4.2 5
escape {{{ $data }}} {{ $data }}
raw {{ $data }} {!! $data !!}

HTML Helper

  • 4.2
    • HTML::
    • FORM::
  • 5 (It’s removed, solution)

    • Adding html package
    • Using original html tag

      <script src="{{ asset("js/jquery.js") }}"></script>
      
      <link rel="stylesheet" href="{{ asset("css/style.css") }}">
      
      <form>
          <input type="hidden" name="_token" value="{{ csrf_token() }}">
          <input type="hidden" name="_method" value="PUT">
      </form>
      

Pagination

Methods

4.2 5
getTotal() total()
links() render()
getCurrentPage() currentPage()
getLastPage() lastPage()
getPerPage() perPage()
getFrom() firstItem()
getTo() lastItem()

Customize

  • 4.2, modify config or dynamic setting

    // app/config/view.php
    'pagination' => 'pagination::slider-3',
    
    // views/news.php
    $news->links('view.name');
    
  • 5

    1. Implement Presenter

      // Illuminate/Pagination/BootstrapThreePresenter.php
      
      use IlluminateContractsPaginationPresenter as PresenterContract;
      
      class BootstrapThreePresenter implements PresenterContract
      {
      }
      
    2. Give render method presenter object

      $news->render($bootstrapThreePresenter);
      

Error Handler

4.2 5
app/start/global.php app/Exceptions/

Package

Auth

  • 4.2, it doesn’t maintain anymore.

    "artdarek/oauth-4-laravel"
    
  • 5 (Official Package: Socialite), Socialite needs your Google app to have Google+ API enabled.

    "laravel/socialite": "~2.0"
    

Predis

  • Adding predis to proeject composer.json require block, In laravel 5, predis move from require to require-dev.

    "predis/predis": "~1.0"
    

Unit Test

diff 4.2 5
test env app/config/testing phpunit.xml

Core Concept

Service Providers

Folder

app/Providers

Register

// app/config/
'providers' => [
    //...
]

Contracts

Repositories

Binding Interfaces To Implementations

// app/Providers/AppServiceProvider.php
public function register()
{
    $this->app->bind(
        'App\Contracts\News',
        'App\Repsitories\EloquentNewsRepository'
    );
}

Remember to Clear Compiled Cache

php artisan clear-compiled

Facades