在网站各处使用自定义功能

在网站各处使用自定义功能

本文介绍了在网站各处使用自定义功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自过程PHP,正在与Laravel学习OOP.到目前为止,我学到的东西非常有趣,并且可以减轻开发人员的生活(这不是我的工作).因此,对于我所有的网站,我对所有文章,类别等都使用了Slug属性.我开始使用Laravel提供的"str_slug",似乎可以完成99%的工作.我得到的问题是当我拥有这样的标题(法语)时:"J'aimangéune pomme",而我遇到的子弹字符串是:"jai-mange-une-pomme",这在法语中是不正确的.我想要"j-ai-mange-une-pomme".

I come from the procedural PHP and am learning OOP with Laravel. What I learned so far is very interesting and will ease my developer's life (it's not my job btw).So, for all my websites, I am using a slug property for all articles, categories, and so on.I started to use the "str_slug" provided by Laravel which seems to do the job at 99%. The issue I get is when I have such title (in french): "J'ai mangé une pomme", the slug string I get is: "jai-mange-une-pomme" which, in french, is not correct. I would like "j-ai-mange-une-pomme".

这不是真正的问题.我可以做到:

It's not really an issue. I can do:

$slug = str_replace('\'','_',$input['name']);
$slug = str_slug($slug, '-');

它非常适合我,但我想知道我想何时使用它.我不想一次又一次地写它.在过程中,这很容易,我会在helpers.php文件中编写一个函数,例如thePerfectSlug(){}(仍然是一个示例),并将在index.php的顶部使用一个include.这样就可以了.

It suits me well but I wonder how to use anytime I want to use it. I don't want to write it again and again and again.In procedural, it's easy, I would write a function, such as thePerfectSlug(){} in a helpers.php file (still an example) and will use an include at the top of my index.php. That would do the job.

但是在OOP中,尤其是在Laravel(5.1)中,我该怎么做?

But in OOP and especially in Laravel (5.1), how can I do that?

谢谢

推荐答案

您仍然可以使用常规功能来实现它. Laravel使用自己的函数,该函数存储在helpers.php文件中.您可以创建自己的helpers.php文件,并将其添加到autoload.files的主composer.json文件中.

You still can achieve it with normal function. Laravel uses his own function which are stored in helpers.php file. You can make your own helpers.php file and add it to your main composer.json file at autoload.files.

如果您想以OOP方式进行操作,请使用您的方法创建一个诸如App \ Traits \ Sluggify之类的特征,并在需要它的任何类中使用它.

If you would like to do it in OOP way, create a trait like App\Traits\Sluggify with your method and use it in any class that needs it.

这篇关于在网站各处使用自定义功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 02:45