第一步:打开routes.PHP文件,把登录进去之后的所有方法放到Route::group(['middleware' => 'auth'], function () {放在此处 });里
例:
//首页
Route::any('/', 'LoginController@login');
Route::any('login', 'LoginController@login');//登录页面
Route::any('address', 'LoginController@address');//第三方登录的回调的方法(地址)
Route::group(['middleware' => 'auth'], function () {//非法登录开始
Route::any('index', 'IndexController@index');//登录成功之后所去的方法
Route::any('summan', 'IndexController@summan');
Route::any('tiyu', 'IndexController@tiyu');
Route::any('xin', 'IndexController@xin');
Route::any('study', 'IndexController@study');
Route::any('city','IndexController@city');
Route::any('comecity','IndexController@comecity');
Route::any('sou','IndexController@sou');
Route::any('search','IndexController@search');
//播放页
Route::any('money', 'MovieController@money');
Route::any('recharge', 'MovieController@recharge');
Route::any('pay', 'MovieController@pay');
Route::any('pay_db', 'MovieController@pay_db');
Route::any('pay_user', 'MovieController@pay_user');
Route::any('fanhui', 'MovieController@fanhui');
Route::any('hot', 'MovieController@hot');
Route::any('movie', 'MovieController@movie');
//用户中心
Route::any('user', 'UserController@user');
Route::any('guanzu', 'UserController@guanzu');
Route::any('user_center','UserController@user_center');
Route::any('lahei','UserController@lahei');
Route::any('loginout', 'LoginController@Loginout');
});//非法登录结束
第二步:在app->Http->Middleware里新建PowerLogin.php文件
内容为:
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Redirect;
use Session;
class PowerLogin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next)
{
session_start();//开启session
//echo $_SESSION['username'];die;
if(!empty($_SESSION['username'])){//判断是否有session值username
//echo "有session值";
return $next($request);
}
else{
//return Redirect('login');
echo "<script>alert('请先去登录');location.href='login'</script>";
}
}
}
第三步:在app->Http->Kernel.php里面的最后一个方法加入一行:'auth' => \App\Http\Middleware\PowerLogin::class,//新加的
如图:
protected $routeMiddleware = ['auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'auth' => \App\Http\Middleware\PowerLogin::class,//新加的,PowerLogin为第二步新建的类名.auth为第一步在routes.php文件里Route::group(['middleware' => 'auth'], function () {//非法登录开始的。