简体   繁体   English

登录用户时需要更改登录URL-Laravel 5.2

[英]Need to change the login url when the user is logged - Laravel 5.2

I have this problem with my login button. 我的登录按钮有这个问题。 When the user is logged I need to change the login in the url with something else, for example admin , student (depends on which user is logged). 登录用户后,我需要使用其他方式更改url中的login ,例如adminstudent (取决于登录的用户)。 The problem is that my button for loggin is in content which I'm using in every page of my website. 问题是我的登录按钮位于我在网站的每个页面中使用的内容中。 Is it even possible to make that? 甚至有可能做到这一点?

Here is the button in my view 这是我认为的按钮

<ul class="nav navbar-nav navbar-right">
    <li class="{{ set_active(['login']) }}">
        <a href="{{ url('login') }}"><span class="glyphicon glyphicon-education"></span> Дневник</a>
    </li>
</ul>

I'm adding my AuthController 我正在添加我的AuthController

<?php

class AuthController {

    public function getLogin() {
        return view('auth.login', [
        ]);
    }

    public function postLogin(\Illuminate\Http\Request $request) {
        if (Auth::attempt([
                    'username' => $request->input('username'),
                    'password' => $request->input('password'),
                    'type' => 'admin'
                ])) {
            return redirect('admin');
        }


        if (Auth::attempt([
                    'username' => $request->input('username'),
                    'password' => $request->input('password'),
                    'type' => 'teacher'
                ])) {
            return redirect('educator/account');
        }

        if (Auth::attempt([
                    'username' => $request->input('username'),
                    'password' => $request->input('password'),
                    'type' => 'student'
                ])) {
            return redirect('stu');
        }


        return redirect('login')->with('message', [
                    'type' => 'danger',
                    'message' => 'Грешно потребителско име или парола!'
        ]);
    }

    public function getLogout() {
        Auth::logout();

        return redirect('login');
    }

You can check if the user is currently authenticated in the system using blade: 您可以使用刀片检查系统中当前是否对该用户进行了身份验证:

<ul class="nav navbar-nav navbar-right">
    <li class="{{ set_active(['login']) }}">
        @if(Auth::check())
            @if(Auth::user()->type == 'student')
            <a href="{{ url('student') }}">
            @elseif (Auth::user()->type == 'admin')
            <a href="{{ url('admin') }}">
            @endif
        @else
            <a href="{{ url('login') }}">
        @endif

            <span class="glyphicon glyphicon-education"></span> Дневник
        </a>
    </li>
</ul>

You might want to consider using named routes, including partial views or sub views and pass the data you want to include in the url of the button. 您可能要考虑使用命名的路由,包括局部视图或子视图,然后将要包含的数据传递到按钮的url中。

For example, your routes. 例如,您的路线。

Route::get('login', ['as' => 'login', 'uses' => 'AuthController@getLogin']);

Route::post('signin', ['as' => 'signin', 'uses' => 'AuthController@postLogin']);

Route::get('admin', ['as' => 'admin', 'uses' => 'AdminController@getAdmin']);

Route::get('educator/teacher', ['as' => 'teacher', 'uses' => 'AdminController@getTeacher']);

In your HomeController. 在您的HomeController中。

public function index()
{
    $type = 'login';

    return view('home', compact('type'));
}

In your AuthController. 在您的AuthController中。

// Auth controller

public function postLogin(Request $request)
{
    if (Auth::attempt([
        'username' => $request->input('username'),
        'password' => $request->input('password')
    ]) {
         $type = Auth::user()->type;

         return redirect()->route($type);
    }

    return redirect('login')->with('message', 'Your message here');
}

In your AdminController. 在您的AdminController中。

public function getAdmin()
{
    $type = Auth::user()->type;

    return view('admin', compact('type'));
}

public function getTeacher()
{
    $type = Auth::user()->type;

    return view('teacher', compact('type'));
}

Then in you define a loginButton.blade.php file in you views 然后在您的视图中定义一个loginButton.blade.php文件

// resources/views/partials/loginButton.blade.php

<ul class="nav navbar-nav navbar-right">
    <li class="{{ set_active(['login']) }}">
        <a href="{{ url({{type}}) }}">
            <span class="glyphicon glyphicon-education"></span>
            Дневник
        </a>
     </li>
</ul>

Then include it in any page you need it as long as the method rendering the view passes the variable $type to the view. 然后将其包含在您需要的任何页面中,只要呈现视图的方法将变量$ type传递给视图即可。 Hope this helps. 希望这可以帮助。

// resources/views/home.blade.php

<div>
    @include('partials.loginButton')

    <!-- Homepage Contents -->
</div>

I fix it by adding this to my Redirect Middleware so that based on the user type it be redirected. 我通过将其添加到我的重定向Middleware来修复它,以便根据用户类型对其进行重定向。

public function handle($request, Closure $next, $guard = null) {
    if (Auth::guard($guard)->check()) {
        if (Auth::user()->type == 'admin') {
            return redirect('admin');
        }
        if (Auth::user()->type == 'student') {
            return redirect('stu');
        }
        if (Auth::user()->type == 'teacher') {
            return redirect('educator/account');
        } else {
            return redirect('login');
        }
    }

    return $next($request);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM