Ilvsx's Blog

return practice() ? '1 week' : 'never';

Laracasts - What’s New in Laravel 5.2

  • 01 Implicit Route Binding

    • bind primary key

      Route::get('users/{user}', function(App\User $user){
        return $user
      });
      
    • bind other key

      Route::get('users/{userName}', function(App\User $user){
        return $user;
      });
      
      // RouteServiceProvider.php
      \Route::bind('userName', function($user_name){
        return \App\User::where('user_name', $user_name)->firstOrFail();
      });
      
  • 02 API Rate Limiting

    Route::get('/api/search/{term}', function($term){
      return [
        'results' => $term
      ];
    })->middleware('throttle:30,5');  // 30 requests per 5 minute
    
    
    // framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php
    $key = $this->resolveRequestSignature($request);
    
    /**
     * Resolve request signature.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string
     */
    protected function resolveRequestSignature($request)
    {
        return $request->fingerprint();
    }
    
    /**
     * Get a unique fingerprint for the request / route / IP address.
     *
     * @return string
     */
    public function fingerprint()
    {
        if (! $this->route()) {
            throw new RuntimeException('Unable to generate fingerprint. Route unavailable.');
        }
        return sha1(
            implode('|', $this->route()->methods()).
            '|'.$this->route()->domain().
            '|'.$this->route()->uri().
            '|'.$this->ip()
        );
    }