Ger
Ger

Reputation: 31

Laravel slug breaks with slash

I have my slug stored in my database. In my controller i retrieve my slug en then add a view to it.

It all works fine if i have a url like domain.com/prices.

But when i have a url like domain.com/prices/somethingelse then the app breaks.

Looks like i cannot handle the /

In my datebase i have the right url..

This is my controller:

  <?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Content;
use App\Models\Product;
use App\Models\Brand;
use App\Models\Thumbnail;
use App\Models\Content_blocks;
use App\Models\Basiscontent;
use App\Models\Contentcategory as Contentcategories;
use App\Models\View as viewValue;
use App\Models\Banners as Banner;
use App\Models\Aanbieding;
use Illuminate\Http\Request;
use Illuminate\View\View as template;
use Illuminate\Support\Facades\View;

class ContentController extends Controller
{
    /**
     * Gebruikerslijst
     *
     * @param Request $request
     * @param string $slug
     * @param string $value
     * 
     * @return template
     */
    public function index(Request $request, string $slug = '', string $value = '', int $id = null): template
    {
        if ($slug == '') {
            $slug = '/';
        }

        $content = Content::getContentBySlug($slug);

        if ($content === null) {
            return abort(404);
        }

        $pageSlug = $content->slug;
        $view = $content->view()->first();

        if ($view === null) {
            $view = 'index';
        } else {
            // $pageView = $content->view()->first()->value;
            // $view = $pageSlug !== $pageView ? 'index' : $pageView;
            $view = $content->view()->first()->value;
        };

       $id = $content->id;

        $content_blocks = Content_blocks::getContentBlock($id);
        $content_block_thumb = Content_blocks::getContentThumb($id);
            // dd(Content_blocks::categorie());
            // dd($contentcategory = Contentcategories::all());

        // $content_block_thumb = Content_blocks::getContentBlock();
        // dd(Content::getContentBySlug($slug)->title);
        // dd(Content_blocks::getContentBlock($id)->id);
        // dd(Thumbnail::getFile(1));

        $content_blocks_cat = Content_blocks::orderBy("order", "ASC");
        // dd($content_blocks_cat->get());

        return view::make($view)
            ->with('products', Product::all()->sortBy('order'))
            ->with('brands', Brand::all())
            ->with('banners', Banner::all())
            ->with('aanbiedingen', Aanbieding::all())
            ->with('contentcategory', Contentcategories::all())
            ->with('basiscontent', Basiscontent::all())
            ->with('content', $content)
            // ->with('content_blocks', Content_blocks::all());
            ->with('content_blocks', $content_block_thumb)
            ->with('content_blocks_cat', $content_blocks_cat->get());
            // ->with('thumbnail', Thumbnail::getFile($content_block_thumb->thumbnail_id));
            // ->with('thumbnail', Thumbnail::getFile(1));
    }

}

Route:

Route::get('{slug?}', '\App\Http\Controllers\ContentController@index');

Hope you can help me out.

Thanks!

Upvotes: 0

Views: 1396

Answers (1)

aynber
aynber

Reputation: 23000

For extra slashes, you need to add another layer check for the route:

Route::get('{slug1}/{slug2?}', '\App\Http\Controllers\ContentController@index');

Add as many for the route depths as needed. You'll need to adjust your function to accept the additional paramters as well.

Upvotes: 1

Related Questions