Laravel框架中扩展函数、扩展自定义类的方法
网络编程 2021-07-05 09:50www.168986.cn编程入门
这篇文章主要介绍了Laravel框架中扩展函数、扩展自定义类的方法,非常实用的技术文章,需要的朋友可以参考下
一、扩展自己的类
在app/ 下建立目录 libraries\class
然后myTest.php 类名格式 驼峰 myTest
代码如下:
<?php
class myTest
{
public function test()
{
return '1asdasd111';
}
}
在 app/start/global.php
代码如下:
ClassLoader::addDirectories(array(
app_path().'/mands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/libraries/class', //增加这一段
));
用 make 载入
代码如下:
<?php
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'; // 引入自定义函数库
我感觉方法一会比较好。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程