php 如何在我的 Laravel 5 应用程序中使用 Illuminate\Support\Str::slug?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29281013/
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-08-26 00:20:12  来源:igfitidea点击:

How to use Illuminate\Support\Str::slug in my Laravel 5 app?

phplaravellaravel-5

提问by Martyn

I'm doing the following in the

我正在做以下

public function boot(DispatcherContract $events)
{
    parent::boot($events);

    // set Tag slug
    Tag::saving(function($tag)
    {
        //slugify name
        $tag->slug = Str::slug($tag->name);
    });
}

When I run it in tinker, I get the following error:

当我在 tinker 中运行它时,出现以下错误:

PHP Fatal error:  Class 'App\Providers\Str' not found in /var/www/questions-l5/app/Providers/EventServiceProvider.php on line 35

..but I don't know the Laravel way to import it. Do I need to just use use, I tried to add the following to the config/app.php file:

..但我不知道 Laravel 导入它的方式。我是否只需要使用use,我尝试将以下内容添加到 config/app.php 文件中:

'aliases' => [
...
'Str'      => 'Illuminate\Support\Str',

.. didn't seem to make much difference though.

..不过似乎没有太大区别。

http://chrishayes.ca/blog/code/laravel-4-generating-unique-slugs-elegantlyhttp://laravel.com/api/5.0/Illuminate/Support/Str.html

http://chrishayes.ca/blog/code/laravel-4-generating-unique-slugs-elegantly http://laravel.com/api/5.0/Illuminate/Support/Str.html

回答by Limon Monte

I don't think you need to create alias here, so just add

我认为你不需要在这里创建别名,所以只需添加

use Illuminate\Support\Str;

to your model.

到你的模型。

回答by stefanzweifel

Edit: As samiles pointed out, str_slug($text)has been removed in Laravel 6.0.

编辑:正如 samiles 所指出的,str_slug($text)已在 Laravel 6.0 中删除。

In Laravel 5 you can use str_slug($text)directly. You no longer have to use the Facade.

在 Laravel 5 中你可以str_slug($text)直接使用。您不再需要使用 Facade。

http://laravel.com/docs/5.1/helpers#method-str-slug

http://laravel.com/docs/5.1/helpers#method-str-slug

回答by michasaurus

I had the exact same issue. Adding the line to aliases in the app\config\app.phpfile is the correct way to fix this.

我有完全相同的问题。将此行添加到app\config\app.php文件中的别名是解决此问题的正确方法。

'alias' => [
     ...
     'Str' => Illuminate\Support\Str::class,
     ...
],

Doing it this way avoids:

这样做可以避免:

  1. the need to add a use Illuminate\Support\Str;line in your blade file
  2. the need to fully quality the class, i.e \Illuminate\Support\Str::slug($tag->name)
  1. 需要use Illuminate\Support\Str;在您的刀片文件中添加一行
  2. 需要充分提高课程质量,即 \Illuminate\Support\Str::slug($tag->name)

In fact you can see that Mr. Otwell includes this line in Laravel 5.8.

事实上你可以看到 Otwell 先生在Laravel 5.8 中包含了这行代码

I'm not sure why this didn't work for you. It could be that your Laravel is out of date. Make sure you've upgraded to 5.8 and then run composer update.

我不确定为什么这对你不起作用。可能是您的 Laravel 已过期。确保您已升级到 5.8,然后运行composer update.

回答by terry low

you can call it by \Str::slug(); you dont have to register an alias to make it works, the Str class is not a facades, but a real static class

你可以通过 \Str::slug() 调用它;您不必注册别名以使其工作,Str 类不是外观,而是真正的静态类

回答by stackMonk

In laravel version 5.4, write like

在 Laravel 5.4 版中,像这样写

Str::slug()

回答by Luca C.

In laravel 5 is simpler, you can use these helpers:

在 laravel 5 中更简单,你可以使用这些助手:

Before Laravel 5.8 (from this version is deprecated):

Laravel 5.8 之前(此版本已弃用):

str_slug($tag->name);

After Laravel 5.4:

在 Laravel 5.4 之后:

Str::slug($tag->name)