Laravel 4中的公共可用功能 [英] Public Available Function in Laravel 4
本文介绍了Laravel 4中的公共可用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我想在Laravel中创建一个函数,该函数在我的应用程序中随处可见,那我将如何处理?
If I would like to make a function in Laravel, that is available everywhere in my app, how would I go about that?
我的意思是,如果我创建一个名为resizeimage()的函数并想在Laravel应用程序中的任何位置调用它,该怎么做?
What I mean, is that, if I create a function called resizeimage() and would like to call it from anywhere in my Laravel application, how can I do this?
推荐答案
创建一个名为:libraries
在您的库中创建文件(类):Image.php
Create a file(class) in your library: Image.php
然后将此代码添加到Image.php
:
<?php
class Image{
public static function resizeImage($image){
return $image;
}
}
在开始"文件夹中编辑global.php
:添加app_path().'/libraries',
Edit global.php
in your 'start'-folder: Add app_path().'/libraries',
示例:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/libraries',
));
现在,您可以像在任何地方"这样调用函数":
Now you can call your 'function' like this 'anywhere':
Image::resizeImage('http://path/to/image');
这篇关于Laravel 4中的公共可用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文