Home Backend Development PHP Tutorial Laravel框架中扩展函数、扩展自定义类的方法_PHP

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

May 31, 2016 pm 07:29 PM
laravel framework extension function Extended class

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'; // 引入自定义函数库

我感觉方法一会比较好。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use model events (Model Events) in Laravel framework How to use model events (Model Events) in Laravel framework Jul 28, 2023 am 10:49 AM

How to use model events (ModelEvents) in Laravel framework Laravel framework provides many powerful features, one of which is model events (ModelEvents). Model events are a feature used in Laravel's EloquentORM (Object Relational Mapping) that allows developers to execute custom code when a specific action occurs on the model. In this article, we will explore how to use model events in the Laravel framework and provide a

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

How to learn Laravel How to learn Laravel for free How to learn Laravel How to learn Laravel for free Apr 18, 2025 pm 12:51 PM

Want to learn the Laravel framework, but suffer from no resources or economic pressure? This article provides you with free learning of Laravel, teaching you how to use resources such as online platforms, documents and community forums to lay a solid foundation for your PHP development journey from getting started to master.

Laravel framework skills sharing Laravel framework skills sharing Apr 18, 2025 pm 01:12 PM

In this era of continuous technological advancement, mastering advanced frameworks is crucial for modern programmers. This article will help you improve your development skills by sharing little-known techniques in the Laravel framework. Known for its elegant syntax and a wide range of features, this article will dig into its powerful features and provide practical tips and tricks to help you create efficient and maintainable web applications.

How to use Task Scheduler to execute scheduled tasks in the Laravel framework How to use Task Scheduler to execute scheduled tasks in the Laravel framework Jul 29, 2023 am 09:54 AM

How to use the task scheduler (TaskScheduler) to execute scheduled tasks in the Laravel framework. With the development of web applications, scheduled tasks play a crucial role in many scenarios. The Laravel framework provides a powerful task scheduler (TaskScheduler) function that can easily perform various scheduled tasks, such as generating reports, cleaning caches, sending emails, etc. This article will introduce how to use the task scheduler to execute scheduled tasks in the Laravel framework.

How to use Attributes to extend the functionality of a class in PHP8? How to use Attributes to extend the functionality of a class in PHP8? Oct 19, 2023 am 09:13 AM

How to use Attributes to extend the functionality of a class in PHP8? With the release of PHP8, new language features Attributes were introduced. Attributes is a feature that adds metadata in the form of annotations in the code. By using Attributes, we can add additional information to elements such as classes, methods, properties, etc. to meet more complex business requirements and development specifications. In this article, we will detail using the Attributes extension in PHP8

How to extend the functionality of classes through Attributes in PHP8? How to extend the functionality of classes through Attributes in PHP8? Oct 25, 2023 am 11:54 AM

How to extend the functionality of classes through Attributes in PHP8? In PHP8, a new feature was introduced - Attributes, also known as attributes. It can add metadata to entities such as classes, class properties, methods, and functions, and these metadata can be accessed and used at runtime. Attributes provide a concise and flexible way to extend the functionality of classes and help developers better organize and understand code. In order to better understand and apply Attributes, the following

How to use PHP extension functions? How to use PHP extension functions? Apr 16, 2024 pm 01:39 PM

PHP extension functions are additional functions beyond the core components that extend the functionality of PHP. After installing extension functions, enable them in php.ini and then use extension functions such as the Imagick extension for processing images. You can install extensions using the command line (PECL), the extensions folder, or Composer, and then use the extension functions in your code via namespaces.

See all articles