Reputation: 31
I've tried a few things already but some reason on my Homestead install of Laravel 6.17 I have a single route of /search that is giving a 404. I expect it to redirect if the user doesn't enter anything into the search field.
I did run the route:list command and got this
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 |
+--------+----------+------------------------+------------------+------------------------------------------------------------------------+------------+
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 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 in my search view
@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
Upvotes: 0
Views: 317
Reputation: 3594
Before "search" route You have another one with "/{channel}" that will match everything You type, also "search", so SearchController is never called:
Route::get('/{channel}','ChannelController@index');
Route::get('/search','SearchController@index')->name('search');
You have 3 options:
Remove this line if its not used (404 is probably called from not existed method in ChannelController or that "channel" that was not found).
If this route is used - the way You typed it is bad practice (and that caused error). It would be better to use 'channel/{channel}'.
If You really need to catch every text typed on level 0 of URL, just put this route AFTER search route, at very end.
Upvotes: 3
Reputation: 4153
Change the order of these lines:
Route::get('/{channel}','ChannelController@index');
Route::get('/search','SearchController@index')->name('search');
You have a route that takes a variable (named channel
) and when you call the search
route, it passes the search
string as channel
variable.
Upvotes: 1