更改 url 的命名约定

我想更改以下网址的命名约定:


http://example.org/designs/CV%20Designs

如下:


http://example.org/designs/cv-designs

这是我的 web.php 路由文件:


    Route::get('designs/{design}', 'DesignsController@show')

        ->name('designs.show');  

这是我的设计模型:


    public function getRouteKeyName()

    {

        $slug = Str::slug('Laravel 5 Framework', '-');

        dd($slug);

        return 'designName';

    }

当我 dd(slug); 输出是“Laravel 5 Framework”,但我希望它是 designName


jeck猫
浏览 143回答 1
1回答

皈依舞

好的,我将在这里做出一些假设,但假设您的设计模型上有这个函数:设计.phpclass Design extends Model{&nbsp; &nbsp; ...&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Assuming you dont have a slug column on your designs table&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* Also assuming the slug is built from a column called 'name' on&nbsp; &nbsp; &nbsp;* your designs table&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function getSlugAttribute()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return \Illuminate\Support\Str::slug($this->name);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // This assumes there is a column on posts table of 'design_id'&nbsp; &nbsp; public function posts()&nbsp; &nbsp; &nbsp; &nbsp; return $this->hasMany(Post::class);&nbsp; &nbsp; }&nbsp; &nbsp; ...}现在让我们举例说明如何构建所需的路线。编辑通过与提问者进一步讨论,他们不想显示与他们所展示的设计相关的所有帖子(参见上面的模型)。这个答案中的设置适合于此,您可以参考下面定义的 show 方法。假设我们有DesignsController.php:class DesignsController extends Controller{&nbsp; &nbsp; ...&nbsp; &nbsp; public function index()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return view('designs.index', [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'designs' => Design::all(),&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; }&nbsp; &nbsp; public function show(Request $request, string $design)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // It is worth noting here if name has a double space you wont&nbsp; &nbsp; &nbsp; &nbsp; // be able to build backwards to it for a query&nbsp; &nbsp; &nbsp; &nbsp; // ie) Design\s\sOne !== $lower(Design\sOne)\&nbsp; &nbsp; &nbsp; &nbsp; $spaced = str_replace('-', ' ', $design);&nbsp; &nbsp; &nbsp; &nbsp; $lower = strtolower($spaced);&nbsp; &nbsp; &nbsp; &nbsp; $design = Design::with('posts')->whereRaw("LOWER(name) = '$lower'")->first();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return view('designs.show', [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'design' => $design,&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; }&nbsp; &nbsp; ...}现在,在“designs/index.blade.php”文件中,您可以执行以下操作:&nbsp; &nbsp; @foreach($designs as $design)&nbsp; &nbsp; &nbsp; &nbsp; <a href="{{ route('designs.show', [ 'design' => $design->slug ]) }}">{{ $design->name }}</a>&nbsp; &nbsp; @endforeach这将按名称列出您的所有设计,并通过其 slug 链接到 designs.show 路线。如果您始终希望在序列化为数组或 json时加载 slug 值,则可以将其添加到模型上受保护的 $appends 数组中。如果您不总是希望附加它,则需要在运行时使用例如$design->append('slug').或者,如果您有一系列设计,您也可以这样做$designs->each->append('slug')。现在,在您的 designs.show Blade 文件中,您可以使用我们使用 Design::with('posts') 加载的关系访问设计的帖子,方法如下:&nbsp; &nbsp; @foreach ($design->posts as $post)&nbsp; &nbsp; &nbsp; &nbsp; <img src="{{ asset('storage/'.$post->postImage) }}">&nbsp; &nbsp; @endforeach
打开App,查看更多内容
随时随地看视频慕课网APP