Methods for extending functions and extending custom classes in the Laravel framework _ php instance

Source: Internet
Author: User
This article mainly introduces the methods of extended functions and extended custom classes in the Laravel framework. It is a very practical technical article. For more information, see 1. Expand your class

Create the libraries \ class directory under app /.

Then myTest. php class name format camper myTest

The Code is as follows:


<? Php
Class myTest
{
Public function test ()
{
Return '1asdasd111 ';
}
}


In app/start/global. php

The Code is as follows:


ClassLoader: addDirectories (array (
App_path (). '/commands ',
App_path (). '/controllers ',
App_path (). '/models ',
App_path (). '/database/seeds ',
App_path (). '/libraries/class', // Add this section
));


Load with make

The Code is as follows:


<? Php
Class HomeController extends BaseController {
Protected $ layout = 'layouts. main ';
Public function index ()
{
$ A = App: make ('mytest'); // usage
Echo $ a-> test ();
}
}

2. Expand your own functions

Create the libraries \ function directory in app /.

Create helper. php

Function Format. Use function_exists as follows to prevent duplicate names with the system.

The Code is as follows:


If (! Function_exists ('test2 '))
{
Function test2 ()
{
Echo 2222222222222222;
}
}

Method 1:

In app/filters. php

The Code is as follows:


App: before (function ($ request)
{
Require app_path (). '/libraries/function/helper. php'; // load custom functions
});

Method 2:

In app/bootstrap/autoglad. php

The Code is as follows:


Require _ DIR _. '/../app/functions. php'; // introduce the custom function library

I think the method will be better.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.