路由错误是由于我的路由中存在组前缀

问题描述

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

public interface IElement
{
    Type ResultType { get; }
    TResult Accept<TResult>(IElementVisitor<TResult> visitor);
}

public class ConstElement : IElement
{
    public object Value { get; set; }
    public Type ResultType => Value?.GetType();
    public TResult Accept<TResult>(IElementVisitor<TResult> visitor)
    {
        return visitor.VisitElement(this);
    }
}

public class BinaryElement : IElement
{
    // Child properties are not strongly typed.
    public IElement Left { get; set; }
    public IElement Right { get; set; }
    public Operand Operand { get; set; }
    public Type ResultType => Operand switch
    {
        Operand.Equal => typeof(bool),Operand.GreaterThan => typeof(bool),Operand.Plus => Left.GetType(),Operand.Multiply => Left.GetType(),_ => throw new NotImplementedException(),};
    public TResult Accept<TResult>(IElementVisitor<TResult> visitor)
    {
        return visitor.VisitElement(this);
    }
}

public enum Operand { Equal,GreaterThan,Plus,Multiply }

public class ConstElementValidator : AbstractValidator<ConstElement>
{
    public ConstElementValidator()
    {
        RuleFor(ele => ele.Value).NotNull().Must(value => (value is double) || (value is TimeSpan));
    }
}

public class BinaryElementValidator : AbstractValidator<BinaryElement>
{
    public BinaryElementValidator()
    {
        // Rules for the element itself
        RuleFor(ele => ele.Left).NotNull();
        RuleFor(ele => ele.Right).NotNull();
        RuleFor(ele => ele).Must(ele => IsValidResultTypeCombination(ele.Left.ResultType,ele.Right.ResultType,ele.Operand));
        // Add rules for child elements here? How?
    }
    private bool IsValidResultTypeCombination(Type left,Type right,Operand operand)
    {
        if (left == typeof(bool) && right != typeof(bool))
            return false;
        // other result type validations...
        return true;
    }
}

public interface IElementVisitor<TResult>
{
    TResult VisitElement(ConstElement element);
    TResult VisitElement(BinaryElement element);
}

public class ValidationVisitor : IElementVisitor<ValidationResult>
{
    public ValidationResult VisitElement(ConstElement element)
    {
        return new ConstElementValidator().Validate(element);
    }

    public ValidationResult VisitElement(BinaryElement element)
    {
        // How to add validation of element.Left and element.Right,// taking into account,that their type is IElement,while Validators are bound to the implementation type?
        var result = new BinaryElementValidator().Validate(element);
        var leftResult = element.Left.Accept(this);
        var rightResult = element.Right.Accept(this);
        // merge leftResult and rightResult with result
        return result;
    }
}

我的CostIncludeController:

    public function index($page)
    {
        $page = Page::where('Pages_Slug_Name',$page)->firstorFail();
        $pages = Page::all();
        return view('admin.pages.page',[
            'page' => $page,],compact('pages'));
    }

我的路线:

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

我的blade.PHP文件

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-@R_361_4045@ion','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');
            });
        });
    }); 
});

带有{page}前缀:

enter image description here

没有{page}前缀:

enter image description here

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

enter image description here

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

enter image description here

解决方法

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

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)来确认错误原因。

,

似乎我们需要在$page中传递CostIncludesController参数。这篇文章回答了同样的问题:

I am having trouble with my route in laravel