简体   繁体   中英

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). 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

<?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.

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.

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

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

In your 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.

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

// 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. 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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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