Installation procedure for web api development using laravel lumen with doctrine
The following packages that will be installed:
- laravel-lumen 5.8
- laravel-doctrine/orm 1.4
- laravel-doctrine migrations 1.1
- laravel-doctrine extensions 1.0
- doctrine-extensions gedmo 2.4
composer create-project --prefer-dist laravel/lumen <PROJECT_DIR_NAME> "5.8.*"
cd <PROJECT_DIR_NAME>
composer update
composer dump-autoload
composer require "laravel-doctrine/orm:1.4.*"
Open the bootstrap/app.php file and register Doctrine Service Provider:
$app->register(LaravelDoctrine\ORM\DoctrineServiceProvider::class);
In the same file bootstrap/app.php, set the class aliases for EntityManager, Registry, and Doctrine
copy the code below and append it just below the Service Providers section.
/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| Register the EntityManager, Registry, and Doctrine Facade Aliases
|
*/
class_alias(EntityManager::class, 'EntityManager');
class_alias(Registry::class, 'Registry');
class_alias(Doctrine::class, 'Doctrine');
Create a directory called config and create the file config/database.php then paste the code below:
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
],
];
Create the file config/doctrine.php then paste the code below:
<?php
return [
/*
|--------------------------------------------------------------------------
| Entity Mangers
|--------------------------------------------------------------------------
|
| Configure your Entity Managers here. You can set a different connection
| and driver per manager and configure events and filters. Change the
| paths setting to the appropriate path and replace App namespace
| by your own namespace.
|
| Available meta drivers: annotations|yaml|xml|config|static_php
|
| Available connections: mysql|oracle|pgsql|sqlite|sqlsrv
| (Connections can be configured in the database config)
|
| --> Warning: Proxy auto generation should only be enabled in dev!
|
*/
'managers' => [
'default' => [
'dev' => env('APP_DEBUG'),
'meta' => env('DOCTRINE_METADATA', 'annotations'),
'connection' => env('DB_CONNECTION', 'mysql'),
'namespaces' => [
'App'
],
'paths' => [
base_path('app')
],
'repository' => Doctrine\ORM\EntityRepository::class,
'proxies' => [
'namespace' => false,
'path' => storage_path('proxies'),
'auto_generate' => env('DOCTRINE_PROXY_AUTOGENERATE', false)
],
/*
|--------------------------------------------------------------------------
| Doctrine events
|--------------------------------------------------------------------------
|
| The listener array expects the key to be a Doctrine event
| e.g. Doctrine\ORM\Events::onFlush
|
*/
'events' => [
'listeners' => [],
'subscribers' => []
],
'filters' => []
]
],
/*
|--------------------------------------------------------------------------
| Doctrine Extensions
|--------------------------------------------------------------------------
|
| Enable/disable Doctrine Extensions by adding or removing them from the list
|
| If you want to require custom extensions you will have to require
| laravel-doctrine/extensions in your composer.json
|
*/
'extensions' => [
//LaravelDoctrine\ORM\Extensions\TablePrefix\TablePrefixExtension::class,
//LaravelDoctrine\Extensions\Timestamps\TimestampableExtension::class,
//LaravelDoctrine\Extensions\SoftDeletes\SoftDeleteableExtension::class,
//LaravelDoctrine\Extensions\Sluggable\SluggableExtension::class,
//LaravelDoctrine\Extensions\Sortable\SortableExtension::class,
//LaravelDoctrine\Extensions\Tree\TreeExtension::class,
//LaravelDoctrine\Extensions\Loggable\LoggableExtension::class,
//LaravelDoctrine\Extensions\Blameable\BlameableExtension::class,
//LaravelDoctrine\Extensions\IpTraceable\IpTraceableExtension::class,
//LaravelDoctrine\Extensions\Translatable\TranslatableExtension::class
],
/*
|--------------------------------------------------------------------------
| Doctrine custom types
|--------------------------------------------------------------------------
*/
'custom_types' => [
'json' => LaravelDoctrine\ORM\Types\Json::class
],
/*
|--------------------------------------------------------------------------
| DQL custom datetime functions
|--------------------------------------------------------------------------
*/
'custom_datetime_functions' => [],
/*
|--------------------------------------------------------------------------
| DQL custom numeric functions
|--------------------------------------------------------------------------
*/
'custom_numeric_functions' => [],
/*
|--------------------------------------------------------------------------
| DQL custom string functions
|--------------------------------------------------------------------------
*/
'custom_string_functions' => [],
/*
|--------------------------------------------------------------------------
| Register custom hydrators
|--------------------------------------------------------------------------
*/
'custom_hydration_modes' => [
// e.g. 'hydrationModeName' => MyHydrator::class,
],
/*
|--------------------------------------------------------------------------
| Enable query logging with laravel file logging,
| debugbar, clockwork or an own implementation.
| Setting it to false, will disable logging
|
| Available:
| - LaravelDoctrine\ORM\Loggers\LaravelDebugbarLogger
| - LaravelDoctrine\ORM\Loggers\ClockworkLogger
| - LaravelDoctrine\ORM\Loggers\FileLogger
|--------------------------------------------------------------------------
*/
'logger' => env('DOCTRINE_LOGGER', false),
/*
|--------------------------------------------------------------------------
| Cache
|--------------------------------------------------------------------------
|
| Configure meta-data, query and result caching here.
| Optionally you can enable second level caching.
|
| Available: acp|array|file|memcached|redis
|
*/
'cache' => [
'default' => env('DOCTRINE_CACHE', 'array'),
'namespace' => null,
'second_level' => false,
],
/*
|--------------------------------------------------------------------------
| Gedmo extensions
|--------------------------------------------------------------------------
|
| Settings for Gedmo extensions
| If you want to use this you will have to require
| laravel-doctrine/extensions in your composer.json
|
*/
'gedmo' => [
'all_mappings' => false
]
];
Optionally, the base_path in config/doctrine.php can be updated for the location of Entity files
'managers' => [
'default' => [
...
'paths' => [
base_path('app/Entities')
],
...
]
],
composer require "laravel-doctrine/migrations:1.1.*"
Open the bootstrap/app.php file and register Doctrine Migration Service Provider:
$app->register(LaravelDoctrine\Migrations\MigrationsServiceProvider::class);
composer require "laravel-doctrine/extensions:1.0.*"
composer require "gedmo/doctrine-extensions:^2.4"
Open the bootstrap/app.php file and register Gedmo Service Provider:
$app->register(LaravelDoctrine\Extensions\GedmoExtensionsServiceProvider::class);
Open the config/doctrine.php file and uncomment the timestamp extension:
'extensions' => [
...
LaravelDoctrine\Extensions\Timestamps\TimestampableExtension::class,
...
],