重定向后,尝试检索会话但会话自动清除在laravel 5.4中

时间:2017-07-26 10:23:51

标签: laravel laravel-5.4

重定向后,我尝试使用'dd(session('key'))'在home()中检索会话但会话自动清除并获得null值在laravel 5.4中我也没有发现任何错误或警告消息。 在重定向会话设置和get工作之前。

我的代码如下:

LoginController.php

<?php

 namespace App\Http\Controllers\admin;

 use Illuminate\Http\Request;
 use App\Http\Controllers\Controller;
 use App\employers;
 use App\Model\admin;
 use Session;
 use DB;
 use Validator;


class LoginController extends Controller
{
    public function index(){
       $users = employers::all();
       return view('user_view',['users'=>$users]);
    }

    public function insert_record(){
    $users = new employers;

    $users->userFname = 'Nikunj';
    $users->userLname = 'Makwana';
    $users->gender = 'Male';
    $users->created_at = time();
    $users->updated_at = time();
    $users->save();

    }

public function login(){

   return view('admin/login',['errors'=>array()]);
}


public function home(){
    dd(session('key'));

}

public function checklogin(Request $request){

    $validator = Validator::make($request->all(), [
        'username' => 'required|email',
        'password' => 'required',
      ]);

    if($validator->fails()) {

        $errors = $validator->errors();

        $validation_errors['username'] = $errors->first('username');
        $validation_errors['password'] = $errors->first('password');

        return view('admin/login',['errors'=>$validation_errors]);

    }

    $input = $request->all(); 
    $username = $input['username'];
    $password = $input['password'];
    $admin_login = admin::where('username', $username)
           ->where('password', $password)
           ->count();
    if($admin_login){
        session(['key' => 'value']);
        return redirect('admin_home');
    }
    else
    {
       return redirect('admin');
    }
}




}

浏览/管理/ login.blade.php

<form method="post" action="{{ 'admin_login' }}">
    {{ csrf_field() }}
    <fieldset>
        <label>
            <span class="block input-icon input-icon-right">
                <input type="text" name="username" class="span12" placeholder="Username" />
                <i class="icon-user"></i>
            </span>
        </label>

        <label>
            <span class="block input-icon input-icon-right">
                <input type="password" name="password" class="span12" placeholder="Password" />
                <i class="icon-lock"></i>
            </span>
        </label>

        <div class="space"></div>

        <div class="clearfix">
            <label class="inline">
                <input type="checkbox" />
                <span class="lbl"> Remember Me</span>
            </label>

            <button type="submit" class="width-35 pull-right btn btn-small btn-primary">
                <i class="icon-key"></i>
                Login
            </button>

        </div>

        <div class="space-4"></div>
    </fieldset>
</form>

路线/ Web.php

<?php



Route::get('/', function () {
    return view('welcome');
});

//For database Query
 Route::get('/employers','UserController@index');
 Route::get('/add_employer','UserController@add_employer');
 Route::match(['get','post'],'/insert_employer    /','UserController@insert_record');


// For Admin
Route::get('/admin','admin\LoginController@login');
Route::get('/admin_logout','admin\LoginController@logout');
Route::get('/admin_home','admin\LoginController@home');
Route::match(['get','post'],'/admin_login/','admin\LoginController@checklogin');

Kernel.php

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

 class Kernel extends HttpKernel
 {

   protected $middleware = [
      '\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class',
    '\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class',
    '\App\Http\Middleware\TrimStrings::class',
    '\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class',
];


    protected $middlewareGroups = [
       'web' => [
           '\App\Http\Middleware\EncryptCookies::class',
           '\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class',
           '\Illuminate\Session\Middleware\StartSession::class',
           '\Illuminate\Session\Middleware\AuthenticateSession::class',
           '\Illuminate\View\Middleware\ShareErrorsFromSession::class',
           //'\App\Http\Middleware\VerifyCsrfToken::class',
           '\Illuminate\Routing\Middleware\SubstituteBindings::class',
        ],

       'api' => [
        'throttle:60,1',
        'bindings',
      ],
    ];


    protected $routeMiddleware = [
        'auth' => '\Illuminate\Auth\Middleware\Authenticate::class',
       'auth.basic' => '\Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class',
       'bindings' => '\Illuminate\Routing\Middleware\SubstituteBindings::class',
       'can' => '\Illuminate\Auth\Middleware\Authorize::class',
       'guest' => '\App\Http\Middleware\RedirectIfAuthenticated::class',
       'throttle' => '\Illuminate\Routing\Middleware\ThrottleRequests::class',
       'Role' => '\App\Http\Middleware\RoleMiddleware::class',
       //'csrf' => 'App\Http\Middleware\VerifyCsrfToken'// add it as a middleware route        
    ];
}

0 个答案:

没有答案