Laravel框架中扩展函数、扩展自定义类的方法
一、扩展自己的类
在app/下建立目录libraries\class
然后myTest.php类名格式驼峰myTest
<?php
classmyTest
{
public functiontest()
{
return'1asdasd111';
}
}
在app/start/global.php
ClassLoader::addDirectories(array( app_path().'/commands', app_path().'/controllers', app_path().'/models', app_path().'/database/seeds', app_path().'/libraries/class',//增加这一段 ));
用make载入
<?php
classHomeControllerextendsBaseController{
protected$layout='layouts.main';
publicfunctionindex()
{
$a=App::make('mytest');//用法
echo$a->test();
}
}
二、扩展自己的函数
在app/下建立目录libraries\function
建立helper.php
函数格式,如下用function_exists,防止与系统重名
if(!function_exists('test2'))
{
functiontest2()
{
echo2222222222222222;
}
}
方法一:
在app/filters.php
App::before(function($request)
{
requireapp_path().'/libraries/function/helper.php';//载入自定义函数
});
方法二:
在app/bootstrap/autolad.php
require__DIR__.'/../app/functions.php';//引入自定义函数库
我感觉方法一会比较好。