【问题标题】:Laravel slug breaks with slashLaravel slug 用斜线打断
【发布时间】:2018-09-18 16:27:57
【问题描述】:

我的 slug 存储在我的数据库中。在我的控制器中,我检索我的 slug 然后向它​​添加一个视图。

如果我有一个像 domain.com/prices 这样的网址,一切都很好。

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

看起来我无法处理 /

在我的数据库中,我有正确的 url..

这是我的控制器:

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

希望你能帮帮我。

谢谢!

【问题讨论】:

  • 它是如何中断的,您会收到什么错误消息?
  • 它将我重定向到 404 页面.. 没有错误消息

标签: php laravel controller


【解决方案1】:

对于额外的斜线,您需要为路线添加另一层检查:

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

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

【讨论】:

  • 他们也可以使用{any:.*},它应该匹配任意数量的斜线。我自己在 Lumen 中使用它,因此可能需要对 Laravel 进行调整...它捕获整个路径,因此您不需要可变数量的参数。
  • 就是这样。谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 2021-09-21
  • 1970-01-01
  • 2019-10-15
  • 1970-01-01
  • 2017-07-31
  • 2021-04-05
相关资源
最近更新 更多