路由错误是由于我的路由中存在组前缀 [英] Routing Error Due to a group prefix in my route

查看:64
本文介绍了路由错误是由于我的路由中存在组前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的路由中,如果我删除了{page}前缀,则可以正常工作,但是当我放入它时,会出现错误.对于其他,它工作正常,但不适用于此路由:Route :: get('/{categories}','AdminVisible \ CostIncludeController @ index');我的AdminPageController:

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'));
    }

我的CostIncludeController:

My CostIncludeController:

    public function index($categories){
        $pages = Page::all();
        $packages = Package::where('slug',$categories)->first();
        return view('admin.pages.costinclude',[
            'packages' => $packages,
        ],compact('pages'));    
    }

我的路线:

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');
            });
        });
    }); 
});

我的blade.php文件:

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 &nbsp;<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

带有{page}前缀:

With {page} prefix:

没有{page}前缀:

Without {page} prefix:

当我做dd()时,带{page}前缀:

With {page} prefix when i do dd():

当我做dd()时,没有{page}前缀:

Without {page} prefix when i do dd():

推荐答案

在CostIncludeController @ index中,添加新变量.路由器希望您处理两个变量.

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'));    
}

在两种情况下,都可以通过在控制器函数内执行 dd($ categories)来确认错误原因.

You can confirm the cause of the error by doing a dd($categories) inside your controller function in both cases.

这篇关于路由错误是由于我的路由中存在组前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆