Although composer allows us to reuse many of the existing class libraries (such as packagist.org), we may still use some incompatible composer packages or class libraries. Alternatively, in a project, we may create a class of libraries, and may not be making plans to become composer package. At this point we can use our own library of unique classes in the following ways.
Add a class that can be instantiated directly
Some classes that need to be used directly in the project can be added to the laravel in the following ways
1. Create Class library files app/libraries/class/myclass.php
2. Write the contents of the file
Copy Code code as follows:
<?php
Class Message {
public static function display () {
}
}
?>
To add a class import path in app/start/globals.php
Copy 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 ',//increase here
));
?>
Add AutoLoad directory to Composer.json
Copy 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"//Increased here
]
},
1. Perform composer Dump-autoload to create an import map
2. Use the class you imported to invoke message directly::d isplay ()
This method is also the way to increase the queue class, many people do not know where the queue processing class in Laravel should be placed, in fact, according to the above method, in the app directory to create a queues directory, and then let it directly instantiate can
Adding functions that can be called directly
Some people prefer 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. Write the contents of the file
Copy Code code as follows:
<?php
Function V ($msg) {
Var_dump ($msg);
}
?>
Add the file to the composer automatic Import list
Copy Code code as follows:
"AutoLoad": {
"Classmap": [
...
],
"Files": [
"App/libraries/function/helper.php"
],
},
Or show require this file in the project. Open app/start/global.php, add at end:
Copy Code code as follows:
Require App_path (). ' /libraries/function/helper.php ';
Personally feel both of these ways OK, if you want to control the time the file is loaded, you can even add the following in the filter.php file
Copy Code code as follows:
App::before (function ($request) {
Require ("{$GLOBALS [' app '] [' path.base ']}/app/libraries/function/helper.php");
});
Use function V (' Hello World ') directly in the project;
Add a slightly more complex class library
Sometimes a class library is more than just a file, so the following approach is better suited to a class library with multiple files and multiple structures.
Create a PSR0 or PSR4 standard directory structure.
Copy Code code 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/search.php is Myapp\search.
Modify AutoLoad in composer
Copy 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, there are certain criteria that make the project structure clear and many people who collaborate on development save a lot of communication costs.