S
S
Sergey Khlopov2020-05-11 09:26:30
PHP
Sergey Khlopov, 2020-05-11 09:26:30

What is the best way to send a 404 to the user if the url before the category is incorrect?

Hello, I have the following route:

Route::get('/katalog/{alias}', 'Web\[email protected]')->where('alias','(.*)?');

And here is the method that fires for this route:
catalogItems method

public function catalogItems($alias)
{
        $arrayUrl = explode("/",$alias);
        $entity = Category::GetByAlias(Arr::last($arrayUrl))->first();
        return view("web.page.entity.entity",[
            "entity" => $entity,
            "children" => $entity->children()->Active()->get(),
            "products" => $entity->products()->Active()->with("images")->get(),
            "breadcrumbs" => Breadcrumbs::make($entity)
        ]);
}


It works well with url like this for example:
домен/katalog/catalog1/catalog1-1
But it works with url like this too:
домен/katalog/qweqweqweqweqweqwe/catalog1-1
Because I take the last alias from the url and search for the category by it.

I now think that I need to домен/katalog/qweqweqweqweqweqwe/catalog1-1return 404 to such a url, I think to do this by checking the current url with an array of ancestor aliases, this array returns the class Breadcrumbs::make($entity) and if there is a difference, then return 404. Please tell me if it is correct to do so? Maybe you can do it differently. Thank you in advance for your response.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question