Skip to content

Instantly share code, notes, and snippets.

@nspalo
Last active February 7, 2021 14:40
Show Gist options
  • Save nspalo/da5f8d5b1f1135fd213644d4bcc5eebb to your computer and use it in GitHub Desktop.
Save nspalo/da5f8d5b1f1135fd213644d4bcc5eebb to your computer and use it in GitHub Desktop.
Laravel Lumen with Doctrine Set-Up

Laravel-Lumen 5.8 with Doctrine 1.4 Set-up

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

Installation and Configuration Procedures:

Laravel Lumen Installation

composer create-project --prefer-dist laravel/lumen <PROJECT_DIR_NAME> "5.8.*"
cd <PROJECT_DIR_NAME>
composer update
composer dump-autoload

Doctrine Installation

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')
           ],
           ...
       ]
   ],

Doctrine-Migration Installation

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);

Doctrine-Extension Installation

composer require "laravel-doctrine/extensions:1.0.*"

Gedmo Doctrine-Extension Installation

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,
       ...
   ],

CHEERS MATE!, Happy coding...

Laravel 5.8 with Doctrine 1.4

Installation and configuration procedures for laravel and doctrine The following packages that will be installed:

  • laravel 5.8
  • laravel-doctrine/orm 1.4
  • laravel-doctrine migrations 1.1
  • laravel-doctrine extensions 1.0
  • doctrine-extensions gedmo 2.4

Installation and Configuration Procedures:

Laravel Installation

composer create-project --prefer-dist laravel/laravel <PROJECT_DIR_NAME> "5.8.*"
cd <PROJECT_DIR_NAME>
composer update
composer dump-autoload

After the installation, check for .env file. If not yet created, copy the .env.example
file to .env then generate an application key using the php artisan key:generate command

Doctrine Installation

composer require "laravel-doctrine/orm:1.4.*"

Open the config/app.php file and add the Doctrine Service Provider for autoloading:

'providers' => [
	...
	/*
	 * Doctrine Service Providers...
	 */
	LaravelDoctrine\ORM\DoctrineServiceProvider::class,
	...
]

In the same file config/app.php, set the class aliases for EntityManager, Registry, and Doctrine
copy the code below and append it just below the Service Providers section.

'aliases' => [
	...
	
	'EntityManager' => LaravelDoctrine\ORM\Facades\EntityManager::class,
	'Registry'      => LaravelDoctrine\ORM\Facades\Registry::class,
	'Doctrine'      => LaravelDoctrine\ORM\Facades\Doctrine::class,
	...	
],

Publish the doctrine and debug config file, use the command below:

php artisan vendor:publish --tag="config"

Optionally, the base_path in config/doctrine.php can be updated for the location of Entity files

'managers'                  => [
       'default' => [
           ...
           'paths'      => [
               base_path('app/Database/Entities')
           ],
           ...
       ]
   ],

Doctrine-Migration Installation

composer require "laravel-doctrine/migrations:1.1.*"

Open the config/app.php file and register the Doctrine Migration Service Provider by adding this
LaravelDoctrine\Migrations\MigrationsServiceProvider::class, line of code as see in the code below:

'providers' => [
	...
	/*
	 * Doctrine Service Providers...
	 */
	LaravelDoctrine\ORM\DoctrineServiceProvider::class,
	LaravelDoctrine\Migrations\MigrationsServiceProvider::class,
	...
]

Publish the doctrine migration config file, use the command below:

php artisan vendor:publish --tag="config"

Doctrine-Extension Installation

composer require "laravel-doctrine/extensions:1.0.*"

Gedmo Doctrine-Extension Installation

composer require "gedmo/doctrine-extensions:^2.4"

Open the config/app.php file and register the Gedmo Doctrine Extension Service Provider by adding this
LaravelDoctrine\Extensions\GedmoExtensionsServiceProvider::class, line of code as see in the code below:

'providers' => [
    ...
    /*
     * Doctrine Service Providers...
     */
    LaravelDoctrine\ORM\DoctrineServiceProvider::class,
    LaravelDoctrine\Migrations\MigrationsServiceProvider::class,
    LaravelDoctrine\Extensions\GedmoExtensionsServiceProvider::class,
    ...
]

Open the config/doctrine.php file and uncomment the timestamp extension:

'extensions'                => [
       ...
       LaravelDoctrine\Extensions\Timestamps\TimestampableExtension::class,
       ...
   ],

Apache - WAMP/XAMP configuration

The following configurations will allow us to run laravel without the using the artisan command php artisan serve.
We can instead use localhost:<PORT_NUMBER> in the browsers' address bar

Port to listen

Locate and open the file httpd.conf file then add a new port to listen

Listen <PORT_NUMBER>

VHost Enrty

Locate and open the file httpd-vhosts.conf then add a new vhost entry

<VirtualHost *:<PORT_NUMBER>>
    ServerName <PROJECT_NAME>.localhost
    DocumentRoot "<INSTALL_LOCATION>/<PROJECT_DIR_NAME>/public/"
    <Directory "<INSTALL_LOCATION>/<PROJECT_DIR_NAME>">
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment