M
M
matros972019-04-03 20:13:20
Laravel
matros97, 2019-04-03 20:13:20

Logout route error?

Hello, I made a logout button from the admin panel, here is the code:

<li><a href="{{ route('logout') }}"><i class="fa fa-sign-out"></i> <span>Выход</span></a></li>

when I click on the link I get this error I'm
5ca4e9ac1da90001295579.png
using the standard authentication method

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
JhaoDa, 2019-04-03
@JhaoDa

What prevents you from looking at the list of routes ( php artisan ro:li) and seeing which method is actually waiting for logout?

S
Sergey Panteleev, 2019-04-03
@s_panteleev

You have everything written in the text of the error)
as it was already written above, php artisan ro:liit says that you are expected to receive a POST request, and you are trying to exit with GET

C
Crash XD, 2019-04-04
@crashxd

Now your logout route is generated Auth::routes();
by default. It is declared via the POST method by default.
You can remove Auth::routes();and define your routes for authorization / registration / reset_password / exit.
Or you can just add your own route, without deleting the standard one. For example, I add this:

Route::get('logout', 'Auth\[email protected]')->name('logout');
It works via GET, but refers to the same function as the native one (i.e. no need to rewrite functions or add controllers).
Another option is to use the standard route which works via POST.
To do this, you will need to change your exit button.
More or less like this:
<form id="logout" action="{{ route('logout') }}" method="POST" style="display:none;">
    @csrf
</form>
<li>
    <button type="submit" form="logout">
        <i class="fa fa-sign-out"></i> <span>Выход</span>
    </button>
</li>
If you don't want a button, you can hang a javascript event on your link:
<form id="logout" action="{{ route('logout') }}" method="POST" style="display:none;">
    @csrf
</form>
<li>
    <a  href="{{ route('logout') }}"
        onclick="event.preventDefault(); document.getElementById('logout').submit();">
            <i class="fa fa-sign-out"></i><span>Выход</span>
    </a>
</li>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question