AI编程助手
AI免费问答

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

高洛峰   2016-12-27 10:47   1496浏览 原创

一、扩展自己的类

在app/ 下建立目录 libraries\class 

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

<?php  
class myTest
{
public  function test()
{
return &#39;1asdasd111&#39;;
}
}

在 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 class HomeController extends BaseController {
    protected $layout = &#39;layouts.main&#39;;
public function index()
{
$a = App::make(&#39;mytest&#39;); // 用法
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'; // 引入自定义函数库

我感觉方法一会比较好。

更多Laravel框架中扩展函数、扩展自定义类的方法相关文章请关注PHP中文网!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。