Laravel 5.5 Package Development

Tutorial how to create a Laravel 5.5 package using the new package auto-discovery feature.

Usage of Published Package

If not already available please install MongoDB first. Then create a new Laravel project.

composer create-project laravel/laravel myproject dev-develop
cd myproject
composer require markustripp/mongo
php artisan vendor:publish
<?phpRoute::get('mongo', function(Request $request) {
$collection = Mongo::get()->mydatabase->mycollection;
return $collection->find()->toArray();
});
Route::get('/', function () {
return view('welcome');
});
mongo
> use mydatabase
> db.mycollection.insert( { "hello": "world" } )

Create New Package

A package is often created together with a Laravel project or test project. I recommend to keep both directories separated but if a team collaborates on the package development you must agree on a common relative path structure.

./package-1
./package-2
./package-3
./project-1
./project-2
./packages/package-1
./packages/package-2
./packages/package-3
./projects/project-1
./projects/project-2

Create Package Directory

The name of the package will be markustripp/mongo (same name is used as Github repository). Therefore I create a package directory with the name “mongo”.

mkdir mongo

Create Composer File

The composer file describes the package and lists all dependencies. The composer init command helps you to create this file.

cd mongo
composer init
composer init for markustripp/mongo project
"autoload": {
"psr-4": {
"Markustripp\\Mongo\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Markustripp\\Mongo\\MongoServiceProvider"
],
"aliases": {
"Mongo": "Markustripp\\Mongo\\MongoFacade"
}
}
}
{
"name": "markustripp/mongo",
"description": "MongoDB driver, auth, passport for Laravel",
"license": "MIT",
"authors": [
{
"name": "Markus Tripp",
"email": "markus@mext.at"
}
],
"require": {
"mongodb/mongodb": "^1.1"
},
"autoload": {
"psr-4": {
"Markustripp\\Mongo\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Markustripp\\Mongo\\MongoServiceProvider"
],
"aliases": {
"Mongo": "Markustripp\\Mongo\\MongoFacade"
}
}
}
}

Implementation of the Package

See the Github repository for the implementation of the package.

Atom view of package implementation

Create an App that Uses the Package

Create a new Laravel 5.5 app:

composer create-project laravel/laravel mongo-app dev-develop
cd mongo-app
"repositories": [
{
"type": "path",
"url": "../mongo",
"options": {
"symlink": true
}
}
],
"require": {
"php": ">=7.0.0",
"fideloper/proxy": "~3.3",
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0",
"markustripp/mongo": "dev-master"
},
composer update
Package operations: 2 installs, 0 updates, 0 removals
- Installing mongodb/mongodb (1.1.2): Loading from cache
- Installing markustripp/mongo (dev-master): Symlinking from ../mongo
Writing lock file
<?phpRoute::get('mongo', function(Request $request) {
$collection = Mongo::get()->mydatabase->mycollection;
return $collection->find()->toArray();
});
Route::get('/', function () {
return view('welcome');
});

Final Thoughts

In my last project I used Laravel passport for auth, oAuth and MongoDB for the application in a hybrid database setup (MySQL and MongoDB). Not perfect but was OK for the limited time I had.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store