laravel php扩展,Laravel框架中扩展函数、扩展自定义类的方法

一、扩展自己的类

在app/ 下建立目录 libraries\class

然后myTest.php 类名格式 驼峰 myTest

class myTest

{

public  function test()

{

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 载入

class HomeController extends BaseController {

protected $layout = 'layouts.main';

public function index()

{

$a = App::make('mytest'); // 用法

echo $a->test();

}

}

二、扩展自己的函数

在app/ 下建立目录 libraries\function

建立helper.php

函数格式,如下用function_exists,防止与系统重名

if (! function_exists('test2'))

{

function test2()

{

echo 2222222222222222;

}

}

方法一:

在 app/filters.php

App::before(function($request)

{

require app_path().'/libraries/function/helper.php'; //载入 自定义函数

});

方法二:

在app/bootstrap/autolad.php

require __DIR__.'/../app/functions.php'; // 引入自定义函数库

我感觉方法一会比较好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值