While Composer allows us to reuse many existing libraries (such as packagist.org in), but we still could use some incompatible composer packages or libraries. Also in a project, we may also create a class library, and may not be the intention to make the composer package. This time we can use their own unique library in the following manner.
Increase directly instantiated
Some need to be used directly in the project category, can be increased in the following ways Laravel
1. Create a class library fileapp/libs/class_libs/Message.php
2. Write the contents of the file
<?php namespace Libs\class_libs; class Message { public static function display() { } } ?>
3. composer.json
increase autoload directory
"autoload": { "classmap": [ "app/database/migrations", "app/database/seeds", "app/libs/class_libs" //在这里增加 ] },
4. Switch to the project directory, execute composer dump-autoload
to create an import map
composer dump-autoload
5. Use your own class imported directly call Message::display()
to
This method is also a method of increasing the queue class, a lot of people do not know Laravel queue handler class should be placed where, in fact, according to the above method,
app
create a directory under thequeues
directory, and then allowed to be directly instantiated
Add function can be called directly
Some people like to use v()
instead var_dump()
, you want to do is also very easy in Laravel
1 / create a function fileapp/libs/function_libs/helper.php
2 / write file contents
<?php
function v($msg){
var_dump($msg);
}
?>
3. Place the file to the composer automatically import list
"autoload": { "classmap": [ ... ], "files": [ "app/libs/function_libs/helper.php" ], },
4. Switch to the project directory, execute composer dump-autoload
to create an import map
composer dump-autoload
Or displayed in the project require
file. Open app/start/global.php
, added at the end:
require app_path().'/libs/function_libs/helper.php';
Personal feeling these two methods are OK, if you want to control the time the file is loaded, and can even filter.php
add the following file
App::before( function( $request ) { require( "{$GLOBALS['app']['path.base']}/app/libs/function_libs/helper.php" ); });
6. function directly in the project v('hello world')
;
Increased slightly more complex class libraries
Sometimes a library is not just a simple file, so the following method is more suitable for the library multiple files multiple structures.
1. Create psr0 or psr4 standard directory structure.
libraries Myapp Search (note directory is capitalized) Search.php SearchFacade.php SearchServiceProvider.php AnotherLib
Myapp/Search/Search.php
The Search
namespace for the class Myapp\Search
.
2. Modify the composer in autoload
"autoload": { "classmap": [ ...... ] , "psr-0": { "Myapp": "app/libraries" } },
3. used in the project new Myapp\Search\Search()
to instantiate a class
to sum up
Although Laravel no kind way to force the best, but there are certain standards can make the project a clear structure, eliminating a lot more than the cost to develop the exchange.