Laravel 6搜索路线给予404

Laravel 6 Search Route Giving 404

我已经尝试了一些东西,但是在我的 Homestead 安装中有一些原因 Laravel 6.17 我有一条 /search 路由给出了 404。如果用户不这样做,我希望它能重定向不要在搜索字段中输入任何内容。

我执行了 运行 route:list 命令并得到了这个

vagrant@homestead:~/www/nettubenew$ php artisan route:list
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+------------+
| Domain | Method   | URI                    | Name             | Action                                                                 | Middleware |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+------------+
|        | GET|HEAD | /                      | home             | App\Http\Controllers\GuestController@index                             | web        |
|        | GET|HEAD | channel/{channel}      |                  | App\Http\Controllers\ChannelController@index                           | web        |
|        | PUT      | channel/{channel}/edit |                  | App\Http\Controllers\ChannelSettingsController@update                  | web,auth   |
|        | GET|HEAD | channel/{channel}/edit |                  | App\Http\Controllers\ChannelSettingsController@edit                    | web,auth   |
|        | POST     | login                  |                  | App\Http\Controllers\Auth\LoginController@login                        | web,guest  |
|        | GET|HEAD | login                  | login            | App\Http\Controllers\Auth\LoginController@showLoginForm                | web,guest  |
|        | POST     | logout                 | logout           | App\Http\Controllers\Auth\LoginController@logout                       | web        |
|        | GET|HEAD | password/confirm       | password.confirm | App\Http\Controllers\Auth\ConfirmPasswordController@showConfirmForm    | web,auth   |
|        | POST     | password/confirm       |                  | App\Http\Controllers\Auth\ConfirmPasswordController@confirm            | web,auth   |
|        | POST     | password/email         | password.email   | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail  | web        |
|        | POST     | password/reset         | password.update  | App\Http\Controllers\Auth\ResetPasswordController@reset                | web        |
|        | GET|HEAD | password/reset         | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web        |
|        | GET|HEAD | password/reset/{token} | password.reset   | App\Http\Controllers\Auth\ResetPasswordController@showResetForm        | web        |
|        | POST     | register               |                  | App\Http\Controllers\Auth\RegisterController@register                  | web,guest  |
|        | GET|HEAD | register               | register         | App\Http\Controllers\Auth\RegisterController@showRegistrationForm      | web,guest  |
|        | GET|HEAD | search                 | search           | App\Http\Controllers\SearchController@index                            | web        |
|        | GET|HEAD | upload                 |                  | App\Http\Controllers\VideoUploadController@index                       | web,auth   |
|        | POST     | video                  |                  | App\Http\Controllers\VideoController@store                             | web,auth   |
|        | GET|HEAD | {channel}              |                  | App\Http\Controllers\ChannelController@index                           | web        |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+------------+

我的web.php

Route::get('/','HomeController@index')->name('home');
Route::get('/','GuestController@index')->name('home');

Auth::routes();

// Route::get('/home', 'HomeController@index')->name('home');

Route::group(['middleware' => ['auth']], function(){
    Route::get('/upload','VideoUploadController@index');

    Route::post('/video','VideoController@store');

    Route::get('/channel/{channel}/edit','ChannelSettingsController@edit');
    Route::put('/channel/{channel}/edit','ChannelSettingsController@update');
});


Route::get('/channel/{channel}','ChannelController@index');
Route::get('/{channel}','ChannelController@index');
Route::get('/search','SearchController@index')->name('search');

SearchController.php

<?php

namespace App\Http\Controllers;

use App\Models\Channel;
use App\Http\Requests;
use Illuminate\Http\Request;

class SearchController extends Controller
{
    public function index(Request $request)
    {
        if(!$request->q){
            return redirect('/');
        }

        $channels = Channel::search($request->q)->take(2)->get();

        return view('search.index', [
            'channels' => $channels
        ]);
    }
}

index.blade.php 在我的搜索视图中

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Search for "{{ Request::get('q') }}"</div>

                <div class="card-body">
                    @if ($channels->count())
                        <h4>Channels</h4>
                        <div class="well">
                            @foreach ($channels as $channel)
                                <div class="media">
                                    <div class="media-left">
                                        <a href="/channel/{{ $channel->slug }}">
                                            <img src="{{ $channel->getImage }}" alt="{{ $channel->name }} image" class="media-object">
                                        </a>
                                    </div>
                                    <div class="media-body">
                                        <a href="/channel/{{ $channel->slug }}" class="media-heading">{{ $channel->name }}</a>
                                        Subscription count
                                    </div>
                                </div>
                            @endforeach
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

更改这些行的顺序:

Route::get('/{channel}','ChannelController@index');
Route::get('/search','SearchController@index')->name('search');

你有一个路由,它接受一个变量(名为 channel),当你调用 search 路由时,它将 search 字符串作为 channel 变量传递。

在 "search" 路由之前您有另一个带有“/{channel}”的路由,它将匹配您键入的所有内容,还有 "search",因此永远不会调用 SearchController:

Route::get('/{channel}','ChannelController@index');
Route::get('/search','SearchController@index')->name('search');

您有 3 个选项:

  1. 如果不使用,请删除此行(404 可能是从 ChannelController 中不存在的方法或未找到的 "channel" 调用的)。

  2. 如果使用此路由 - 您输入它的方式是不好的做法(并且会导致错误)。最好用'channel/{channel}'.

  3. 如果您真的需要捕获在 URL 的级别 0 上键入的每个文本,只需将此路由放在搜索路由之后,最后。