LARAVEL 5.1
SESSION 02
Muhammad Rizwan Arshad
Principal Software Engineer, Nextbridge.
July 28, 2015
HTTP Middleware
Introduction
- Authentication
- CSRF protection
app/Http/Middleware
Defining Middleware
# php artisan make:middleware OldMiddleware
namespace App\Http\Middleware; use Closure; class OldMiddleware { public function handle($request, Closure $next) { if ($request->input('age') <= 200) { return redirect('home'); } return $next($request); } }
Before / After Middleware
namespace App\Http\Middleware;
use Closure;
class BeforeMiddleware
{
public function handle($request, Closure $next)
{
return $next($request);
}
}
Before / After Middleware
namespace App\Http\Middleware;
use Closure;
class AfterMiddleware
{
public function handle($request, Closure $next)
{
$response = $next($request);
return $response;
}
}
Global Middleware
For every HTTP request to your application Simply list the middleware class in the $middleware property of your app/Http/Kernel.php class.
Assigning Middleware To Routes
// Within App\Http\Kernel Class...
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
];
Route::get('admin/profile', ['middleware' => 'auth', function () {
//
}]);
Middleware Parameters
class RoleMiddleware { public function handle($request, Closure $next, $role) { if (! $request->user()->hasRole($role)) { // Redirect... } return $next($request); } } Route::put('post/{id}', ['middleware' => 'role:editor', function ($id) {
//
}]);
HTTP Controllers
Introduction
-
handling logic in a single routes.php
- Organize this behavior using Controller classes
- app/Http/Controllers directory
Basic Controllers
class UserController extends Controller
{
public function showProfile($id)
{
return view('user.profile', ['user' => User::findOrFail($id)]);
}
}
Route::get('user/{id}', 'UserController@showProfile');
Controllers & Namespaces
If your full controller class is App\Http\Controllers\Photos\AdminController, you would register a route like so:
Route::get('foo', 'Photos\AdminController@method');
Naming Controller Routes
Route::get('foo', ['uses' => 'FooController@method', 'as' => 'name']);
$url = action('FooController@method');
$url = route('name');
Controller Middleware
Route::get('profile', [ 'middleware' => 'auth', 'uses' => 'UserController@showProfile' ]); class UserController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('log', ['only' => ['fooAction', 'barAction']]);
$this->middleware('subscribed', ['except' => ['fooAction', 'barAction']]);
}
}
RESTful Resource Controllers
# php artisan make:controller PhotoController
Above command will create: app/Http/Controllers/PhotoController.php
Register a resourceful route:
Route::resource('photo', 'PhotoController');
Actions Handled By Resource Controller
Verb Path Action Route
GET | /photo | index | photo.index |
GET | /photo/create | create | photo.create |
POST | /photo | store | photo.store |
GET | /photo/{photo} | show | photo.show |
GET | /photo/{photo}/edit | edit | photo.edit |
PUT/PATCH | /photo/{photo} | update | photo.update |
DELETE | /photo/{photo} | destroy |
photo.destroy |
Partial Resource Routes
Route::resource('photo', 'PhotoController',
['only' => ['index', 'show']]);
Route::resource('photo', 'PhotoController',
['except' => ['create', 'store', 'update', 'destroy']]);
Naming Resource Routes
Route::resource('photo', 'PhotoController',
['names' => ['create' => 'photo.build']]);
Nested Resources
Route::resource('photos.comments', 'PhotoCommentController');
URL: photos/{photos}/comments/{comments}
class PhotoCommentController extends Controller
{
public function show($photoId, $commentId)
{
//
}
}
Supplementing Resource Controllers
you should define related routes before your call to Route::resource;
Route::get('photos/popular', 'PhotoController@method');
Route::resource('photos', 'PhotoController');
Implicit Controllers
Route::controller('users', 'UserController');
class UserController extends Controller
{
public function getIndex()
{}
public function getShow($id)
{}
public function getAdminProfile()
{}
public function postProfile()
{}
}
Assigning Route Names
If you would like to name some of the routes on the controller, you may pass an array of names as the third argument to the controller method:
Route::controller('users', 'UserController', [
'getShow' => 'user.show',
]);
Your Feedback !
LARAVEL 5.1 SESSION 02 Muhammad Rizwan Arshad Principal Software Engineer, Nextbridge. July 28, 2015
Laravel 5.1 - Session 02
By gr8rizwan
Laravel 5.1 - Session 02
- 853