Commit e0507562 by hfpp2012 yinsigan

middleware

1 parent 40f36deb
...@@ -56,5 +56,6 @@ class Kernel extends HttpKernel ...@@ -56,5 +56,6 @@ class Kernel extends HttpKernel
'can' => \Illuminate\Auth\Middleware\Authorize::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'authenticated' => \App\Http\Middleware\AuthenticatedMiddleware::class
]; ];
} }
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
class AuthenticatedMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(Auth::check())
return $next($request);
return redirect('/login');
}
}
...@@ -18,7 +18,7 @@ $factory->define(App\User::class, function (Faker\Generator $faker) { ...@@ -18,7 +18,7 @@ $factory->define(App\User::class, function (Faker\Generator $faker) {
return [ return [
'name' => $faker->name, 'name' => $faker->name,
'email' => $faker->unique()->safeEmail, 'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'), 'password' => $password ?: $password = 'secret',
'remember_token' => str_random(10), 'remember_token' => str_random(10),
]; ];
}); });
...@@ -32,7 +32,7 @@ Route::get('blade', 'PagesController@blade'); ...@@ -32,7 +32,7 @@ Route::get('blade', 'PagesController@blade');
// return $users; // return $users;
// }); // });
Route::get('users', 'UsersController@index'); Route::get('users', 'UsersController@index')->middleware('authenticated');
Route::get('users/create', 'UsersController@create'); Route::get('users/create', 'UsersController@create');
Route::post('users', 'UsersController@store'); Route::post('users', 'UsersController@store');
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!