在laravel中登录后显示登录信息

时间:2016-08-25 12:54:38

标签: php login laravel-5.2

您好我正在使用laravel登录我创建了自己的用户表的任何用户,代码如下:

function login(Request $request) {
        $this->middleware('auth');
        $rules = array(
            'user_login' => 'Required',
            'user_login_password' => 'Required'
        );
        $validator = Validator::make($request->all(), $rules);
        if (!$validator->fails()) {
            if (Auth::attempt(['email' => base64_decode(base64_decode(trim($request->input('user_login')))), 'password' => base64_decode(base64_decode($request->input('user_login_password')))])) {
                die('1');
            } else {
                echo 'Incorrect email or password.';
                die;
            }
        } else {
            echo implode('<br/>', $validator->errors()->all());
            die;
        }
    }

现在我通过Auth :: user() - &gt;电子邮件登录后显示下一页中的数据。但是只是显示错误如下:所以请帮我解决问题,

ErrorException in 0e5a47fe67787924c88247a752bd05e24b49d01c.php line 51: Trying to get property of non-object (View: /var/www/html/recore_web/resources/views/templates/administrator/header.blade.php) (View: /var/www/html/recore_web/resources/views/templates/administrator/header.blade.php) (View: /var/www/html/recore_web/resources/views/templates/administrator/header.blade.php)

这是我的header.blade.php代码..

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
    <title><?php echo isset($title) ? $title : (($this->router->method === 'index') ? '' : ucwords(str_replace('_', ' ', $this->router->method))) . ' ' . ucwords(str_replace('_', ' ', $this->router->class)); ?></title>
        <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
        <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
        <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
        <link href="//code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet" type="text/css" />
        <link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic" rel="stylesheet" type="text/css">
        <link href="<?php echo url('/'); ?>assets/css/base.css" rel="stylesheet" type="text/css" />
        <link href="<?php echo url('/'); ?>assets/css/skins/_all-skins.css" rel="stylesheet" type="text/css" />
        <link href="<?php echo url('/'); ?>assets/css/custom.css" rel="stylesheet" type="text/css" />
        <script src="//code.jquery.com/jquery.min.js"></script>
        <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js" type="text/javascript"></script>
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
            <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->
        <script type="text/javascript">var base_url = '<?php echo url('/'); ?>';</script>
    </head>

2 个答案:

答案 0 :(得分:1)

请验证路由是否在 web 中间件下。像这样:

Route::group(['middleware' => 'web'], function(){
    Route::post('login, 'YourController@login');
});

Web中间件提供会话&amp; cookie,用于对用户进行身份验证并在以后的请求中记住。如果是这种情况,Auth :: user()将为null。

您也可以在调用Auth :: user() - &gt;电子邮件之前尝试Auth :: check()。

@if(Auth::check)
    {{ Auth::user()->email }}
@endif
    User not logged in
@endif

答案 1 :(得分:0)

请检查header.blade.php

中的布局文件夹

您只需打开“0e5a47fe67787924c88247a752bd05e24b49d01c.php”找到该代码,然后查找第51行。

如果您使用崇高的文本编辑器,只需按Ctrl + P键入文件名“0e5a47fe67787924c88247a752bd05e24b49d01c.php并检查第51行。

在administrator / header.blade.php中尝试获取数据,但不传递/创建对象。

您可以发布header.blade.php,以便我为您提供更多指导。