问题
In my routing if i remove the {page} prefix it works completely fine but when i put it i get an error. For other it is working fine but it is not working for this route: Route::get('/{categories}', 'AdminVisible\CostIncludeController@index'); My AdminPageController:
public function index($page)
{
$page = Page::where('Pages_Slug_Name',$page)->firstorFail();
$pages = Page::all();
return view('admin.pages.page',[
'page' => $page,
],compact('pages'));
}
My CostIncludeController:
public function index($categories){
$pages = Page::all();
$packages = Package::where('slug',$categories)->first();
return view('admin.pages.costinclude',[
'packages' => $packages,
],compact('pages'));
}
My Route:
Auth::routes(['register' => false,'login' => false]);
Route::prefix('admin')->group(function() {
Route::get('/')->name('login')->uses('Auth\LoginController@showLoginForm');
Route::post('/')->name('login')->uses('Auth\LoginController@login');
Route::get('/dashboard', 'AdminVisible\HomeController@index')->name('admin.dashboard');
Route::prefix('pages')->group(function() {
Route::get('/','AdminVisible\AdminPageController@pages')->name('pages');
Route::prefix('{page}')->group(function() {
Route::get('/','AdminVisible\AdminPageController@index')->name('page');
Route::get('/banner', 'AdminVisible\BannerController@index');
Route::get('/why-with-us', 'AdminVisible\WhyWithUsController@index');
Route::get('/testimonials', 'AdminVisible\TestimonialsController@index');
Route::get('/about', 'AdminVisible\AboutController@index');
Route::get('/about-why-with-us', 'AdminVisible\AboutWhyWithUsController@index');
Route::get('/general-information', 'AdminVisible\PackageController@index');
Route::get('/package-program', 'AdminVisible\PackageController@index');
Route::get('/cost-exclude', 'AdminVisible\PackageController@index');
Route::prefix('cost-include')->group(function() {
Route::get('/', 'AdminVisible\PackageController@index');
Route::get('/{categories}', 'AdminVisible\CostIncludeController@index');
});
});
});
});
My blade.php file:
@extends('layouts.app')
@section('style')
<link href="{{ asset('css/Admin/sql-data-viewer.css') }}" rel="stylesheet">
<style></style>
@endsection
@section('content')
<section class="data-viewer">
<div class="d-flex justify-content-between">
<h3>Select Package to change</h3>
<a href="#"><button type="button" class="btn add-data text-white rounded-pill">Add <i class="fas fa-plus"></i></button></a>
</div>
<table>
<thead>
<tr class="data-head">
<td scope="col" style="width: 5%"><input type="checkbox"></td>
<th scope="col" style="width: 8.7%">Id</th>
<td scope="col">Includes</td>
</tr>
</thead>
<tbody>
@foreach ($packages->costIncludes as $include)
<tr class="data">
<td scope="col" style="width: 6.5%"><input type="checkbox"></td>
<th scope="col" style="width: 10%;"><a href="">{{$include->id}}</a></th>
<td scope="col" class="text-justify" style="width:696px">{{Str::limit($include->Cost_Include,100)}}</td>
</tr>
@endforeach
</tbody>
</table>
</section>
@endsection
With {page} prefix:
Without {page} prefix:
With {page} prefix when i do dd():
Without {page} prefix when i do dd():
回答1:
In your CostIncludeController@index, add the new variable. The router is expecting you to handle two variables.
public function index($page, $categories){
$pages = Page::all();
$packages = Package::where('slug',$categories)->first();
return view('admin.pages.costinclude',[
'packages' => $packages,
],compact('pages'));
}
You can confirm the cause of the error by doing a dd($categories)
inside your controller function in both cases.
回答2:
It seems we need to pass $page
parameter in the CostIncludesController
. This same question is answered in this post:
I am having trouble with my route in laravel
来源:https://stackoverflow.com/questions/63408857/routing-error-due-to-a-group-prefix-in-my-route