In Laravel, you can use the three methods used to compile the class library: laravel class library. Laravel uses three methods for writing class libraries. Although Composer allows us to reuse many existing class libraries (such as packagist.org ), however, we may still use three methods of writing class libraries in Laravel.
Although Composer allows us to reuse many existing class libraries (such as packagist.org), we may still use some packages or class libraries that are not compatible with composer. In addition, a certain type of library may be created in a project, and it may not be intended to become a composer package. In this case, we can use our own class library in the following ways.
Add classes that can be directly instantiated
Some classes that need to be used directly in the project can be added to Laravel in the following ways
1. create a class library file app/libraries/class/myClass. php
2. Write file content
The code is as follows:
<? Php
Class Message {
Public static function display (){
}
}
?>
Add the class import path to app/start/globals. php.
The code is as follows:
<? Php
ClassLoader: addDirectories (array (
App_path (). '/commands ',
App_path (). '/controllers ',
App_path (). '/models ',
App_path (). '/database/seeds ',
App_path (). '/libaries/class', // add
));
?>
Add the autoload directory to composer. json.
The code is as follows:
"Autoload ":{
"Classmap ":[
"App/commands ",
"App/controllers ",
"App/models ",
"App/database/migrations ",
"App/database/seeds ",
"App/tests/TestCase. php ",
"App/libraries/class" // added here
]
},
1. execute composer dump-autoload to create the import ING.
2. use the imported class to directly call Message: display ().
This method also adds the queue class. many people do not know where the queue processing class should be stored in Laravel. In fact, create a queues directory under the app directory according to the above method, then let it be instantiated directly.
Add functions that can be called directly
Some people prefer v () to replace var_dump (). It is also very easy to do this in Laravel.
1. create a function file app/libraries/function/helper. php
2. Write file content
The code is as follows:
<? Php
Function v ($ msg ){
Var_dump ($ msg );
}
?>
Add the file to the composer automatic import list.
The code is as follows:
"Autoload ":{
"Classmap ":[
...
],
"Files ":[
"App/libraries/function/helper. php"
],
},
Or display the require file in the project. Open app/start/global. php and add:
The code is as follows:
Require app_path (). '/libraries/function/helper. php ';
I personally feel that both methods are OK. if you want to control the file loading time, you can even add the following content to the filter. php file:
The code is as follows:
App: before (function ($ request ){
Require ("{$ GLOBALS ['app'] ['path. base']}/app/libraries/function/helper. php ");
});
Directly use the function v ('Hello World') in the project ');
Add a slightly complex class library
Sometimes a class library is not just a file, so the following method is more suitable for class libraries with multiple structures of multiple files.
Create a standard directory structure of psr0 or psr4.
The code is as follows:
Libraries
Myapp
Search (note directory is capitalized)
Search. php
SearchFacade. php
SearchServiceProvider. php
AnotherLib
The namespace of the Search class in Myapp/Search. php is Myapp \ Search.
Modify autoload in composer
The code is as follows:
"Autoload ":{
"Classmap ":[
"App/commands ",
"App/controllers ",
"App/models ",
"App/libraries ",
"App/database/migrations ",
"App/database/seeds ",
"App/tests/TestCase. php"
]
,
"Psr-0 ":{
"Myapp": "app/libraries"
}
},
Use new Myapp \ Search () in the project to instantiate a category
Summary
Although Laravel does not force the best method, there are some standards that can make the project structure clear, and many people in cooperative development save a lot of communication costs.
Although Composer allows us to reuse many existing class libraries (such as packagist.org), we may still use...