Although composer allows us to reuse many existing class libraries (for example, packagist.org), we may still use packages or class libraries that are incompatible with composer. Also in a project, we may create a class of libraries, and may not be prepared to become the composer package. At this time we can use our own unique class library in the following ways.
Adding classes that can be instantiated directly
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. Writing file contents
Copy the Code code as follows:
<?php
Class Message {
public static function display () {
}
}
?>
Adding the class import path in app/start/globals.php
Copy the Code code as follows:
<?php
Classloader::adddirectories (Array (
App_path (). ' /commands ',
App_path (). ' /controllers ',
App_path (). ' /models ',
App_path (). ' /database/seeds ',
App_path (). ' /libaries/class ',//Add here
));
?>
Add AutoLoad directory to Composer.json
Copy the Code code as follows:
"AutoLoad": {
"Classmap": [
"App/commands",
"App/controllers",
"App/models",
"App/database/migrations",
"App/database/seeds",
"App/tests/testcase.php",
"App/libraries/class"//Add here
]
},
1. Execute composer Dump-autoload to create an import map
2. Call the message directly using the class you imported::d isplay ()
This method is also a way to increase the queue class, many people do not know where the queue processing class should be placed in the laravel, in fact, according to the above method, in the app directory to create a queues directory, and then let it be directly instantiated can
Adding functions that can be called directly
Someone likes to use V () instead of Var_dump (), and it's very easy to do so in Laravel.
1. Create a function file app/libraries/function/helper.php
2. Writing file contents
Copy the Code code as follows:
<?php
Function V ($msg) {
Var_dump ($msg);
}
?>
Add the file to the composer Auto-import list
Copy the Code code as follows:
"AutoLoad": {
"Classmap": [
...
],
"Files": [
"App/libraries/function/helper.php"
],
},
Or display require this file in the project. Open app/start/global.php, add at the end:
Copy the Code code as follows:
Require App_path (). ' /libraries/function/helper.php ';
Personal feeling both methods are OK, if you want to control the file load time, you can even add the following in the filter.php file
Copy the Code code as follows:
App::before (function ($request) {
Require ("{$GLOBALS [' app '] [' path.base ']}/app/libraries/function/helper.php ');
});
Use function v directly in the project (' Hello World ');
Add a slightly more complex class library
Sometimes a class library is more than just a file, so the following approach is more suitable for class libraries with multiple files and multiple structures.
Create a PSR0 or PSR4 standard directory structure.
Copy the Code code as follows:
Libraries
Myapp
Search (note directory is capitalized)
search.php
searchfacade.php
searchserviceprovider.php
Anotherlib
The myapp/search/search.php namespace for the Search class is myapp\search.
Modify Composer in AutoLoad
Copy the Code code 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\search () in your project to instantiate a class of
Summarize
Although Laravel does not enforce which way is best, but there are certain standards can make the project structure is clear, multi-person cooperation development, save a lot of communication costs.