我已经尝试了一些东西,但出于某种原因,在我安装Laravel 6.17的Homestead上,我有一个单一的/search路径,它给出了404。如果用户没有在搜索字段中输入任何内容,我希望它能重定向。
我确实运行了route:list命令,得到了这个
vagrant@homestead:~/www/nettubenew$ php artisan route:list
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+------------+
| | GET|HEAD | / | home | AppHttpControllersGuestController@index | web |
| | GET|HEAD | channel/{channel} | | AppHttpControllersChannelController@index | web |
| | PUT | channel/{channel}/edit | | AppHttpControllersChannelSettingsController@update | web,auth |
| | GET|HEAD | channel/{channel}/edit | | AppHttpControllersChannelSettingsController@edit | web,auth |
| | POST | login | | AppHttpControllersAuthLoginController@login | web,guest |
| | GET|HEAD | login | login | AppHttpControllersAuthLoginController@showLoginForm | web,guest |
| | POST | logout | logout | AppHttpControllersAuthLoginController@logout | web |
| | GET|HEAD | password/confirm | password.confirm | AppHttpControllersAuthConfirmPasswordController@showConfirmForm | web,auth |
| | POST | password/confirm | | AppHttpControllersAuthConfirmPasswordController@confirm | web,auth |
| | POST | password/email | password.email | AppHttpControllersAuthForgotPasswordController@sendResetLinkEmail | web |
| | POST | password/reset | password.update | AppHttpControllersAuthResetPasswordController@reset | web |
| | GET|HEAD | password/reset | password.request | AppHttpControllersAuthForgotPasswordController@showLinkRequestForm | web |
| | GET|HEAD | password/reset/{token} | password.reset | AppHttpControllersAuthResetPasswordController@showResetForm | web |
| | POST | register | | AppHttpControllersAuthRegisterController@register | web,guest |
| | GET|HEAD | register | register | AppHttpControllersAuthRegisterController@showRegistrationForm | web,guest |
| | GET|HEAD | search | search | AppHttpControllersSearchController@index | web |
| | GET|HEAD | upload | | AppHttpControllersVideoUploadController@index | web,auth |
| | POST | video | | AppHttpControllersVideoController@store | web,auth |
| | GET|HEAD | {channel} | | AppHttpControllersChannelController@index | web |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+------------+
My-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 AppHttpControllers;
use AppModelsChannel;
use AppHttpRequests;
use IlluminateHttpRequest;
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
在"搜索"路线之前,您有另一个带有"/{channel}"的路线,它将与您键入的所有内容相匹配,也是"搜索",因此永远不会调用SearchController:
Route::get('/{channel}','ChannelController@index');
Route::get('/search','SearchController@index')->name('search');
您有3个选项:
如果未使用此行,请删除此行(404可能是从ChannelController中不存在的方法或未找到的"通道"调用的(。
如果使用这个路由-你键入的方式是不好的做法(这导致了错误(。最好使用"channel/{channel}"。
如果你真的需要捕捉在URL的0级上键入的每一条文本,只需在搜索路线的最后放上这条路线。
更改这些行的顺序:
Route::get('/{channel}','ChannelController@index');
Route::get('/search','SearchController@index')->name('search');
您有一个采用变量(名为channel
(的路由,当您调用search
路由时,它会将search
字符串作为channel
变量传递。