string 带有 Str::slug 的 Laravel Slugs

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17400788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:00:32  来源:igfitidea点击:

Laravel Slugs with Str::slug

stringurllaravel-4friendly-urlslug

提问by Gareth Daine

Looking at Str::slug for my frontend URL generation but just wondering how you guys go about implementing it with routes etc, for example, how would you guys go about changing http://www.example.com/courses/1to http://www.example.com/courses/this-course

查看 Str::slug 以生成我的前端 URL,但只是想知道你们如何使用路由等实现它,例如,你们将如何将http://www.example.com/courses/1更改为http ://www.example.com/courses/this-course

回答by Gareth Daine

OK, I did it this way:

好的,我是这样做的:

// I have a slug field in my courses table and a slug field in my categories table, along with a category_id field in my courses table.

// Route 

Route::get('courses/{categorySlug}/{slug?}', function($categorySlug, $slug) {
    $course = Course::leftJoin('categories', 'categories.id', 'courses.category_id')
        ->where('categories.slug', $categorySlug)
        ->where('courses.slug', $slug)
        ->firstOrFail();

    return View::make('courses.show')->with('course', $course);
});

Works like a charm. It gets the $categorySlug and $slug variables then uses them to filter the Eloquent model Course to get the correct course object from the database.

奇迹般有效。它获取 $categorySlug 和 $slug 变量,然后使用它们来过滤 Eloquent 模型 Course 以从数据库中获取正确的课程对象。

EDIT: You can generate a URL in your view like:

编辑:您可以在您的视图中生成一个 URL,如:

http://www.example.com/courses/it-training/mcse

http://www.example.com/courses/it-training/mcse

By doing something like:

通过做类似的事情:

<a href="{{ URL::to('courses/'.$course->category->parentCategorySlug($course->category->parent_id).'/'.$course->category->slug.'/'. $course->slug) }}" title="{{ $course->title }}">{{ $course->title }}</a>

A have a method in my Category like below that retrieves the parent category slug. This could be better achieved though using some sort of presenter class which would allow you to simply use $course->url but I haven't got around to doing this yet. I will update the answer when I do.

在我的类别中有一个方法,如下所示,可以检索父类别 slug。虽然使用某种演示者类可以更好地实现这一点,该类允许您简单地使用 $course->url 但我还没有时间这样做。当我这样做时,我会更新答案。

public function parentCategorySlug($parentId)
{
    if ($parentId === '0')
    {
        return $this->slug;
    }

    return $this->where('id', $parentId)->first()->slug;
}

回答by cawecoy

回答by Mr. Crowley

As for me I created a helper function and used the following method taken from here.

至于我,我创建了一个辅助函数并使用了从这里获取的以下方法。

 public static function getSlug($title, $model) {
    $slug = Str::slug($title);
    $slugCount = count( $model->whereRaw("url REGEXP '^{$slug}(-[0-9]*)?$'")->get() );
    return ($slugCount > 0) ? "{$slug}-{$slugCount}" : $slug;
}

回答by reshetech

You can create a related model Slug, and approach the course in your methods like so:

您可以创建一个相关的模型 Slug,并在您的方法中处理课程,如下所示:

$course = Slug::where('slug', $slug) -> firstOrFail() -> course;

回答by deepika jain

I have also implemented a similar URL mapping but I preferred to have both the ID and the slug in the requested URL, like this:

我也实现了类似的 URL 映射,但我更喜欢在请求的 URL 中同时包含 ID 和 slug,如下所示:

http://www.example.com/courses/1/my-laravel-course

http://www.example.com/courses/1/my-laravel-course

This method allows me to get the requested courseobject from the ID given in the URL, rather than having to store the slugs in my DB table.

此方法允许我course从 URL 中给定的 ID 中获取请求的对象,而不必将 slug 存储在我的数据库表中。

Route::post('courses/(:num)/(:any)', function ($courseid, $slug) {
    $course = Course::where('id', '=', $courseid)->get();
    return View::make('courses.show')->with('course', $course);
}