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.