Laravel弹头用斜线打破

时间:2018-09-18 16:27:58

标签: php laravel controller

我的子弹存储在数据库中。在我的控制器中,我检索了我的子弹,然后向其中添加视图。

如果我有一个domain.com/prices之类的网址,一切正常。

但是当我有一个domain.com/prices/somethingelse这样的网址时,应用程序就会崩溃。

看起来我无法处理/

在我的数据库中,我有正确的网址。

这是我的控制者:

  <?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::get('{slug?}', '\App\Http\Controllers\ContentController@index');

希望你能帮助我。

谢谢!

1 个答案:

答案 0 :(得分:0)

要获得额外的斜线,您需要为该路线添加另一层检查:

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

根据需要添加尽可能多的路线深度。您还需要调整功能以接受其他参数。