ProAI/lumen-annotations

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest Stable VersionTotal DownloadsLatest Unstable VersionLicense

This package enables annotations in Laravel Lumen to define routes and event bindings.

Lumen Annotations is distributed as a composer package. So you first have to add the package to your composer.json file:

"proai/lumen-annotations": "~1.0"

Then you have to run composer update to install the package. Once this is completed, you have to add the service provider in bootstrap/app.php:

$app->register(ProAI\Annotations\AnnotationsServiceProvider::class);

Copy config/annotations.php from this package to your configuration directory to use a custom configuration file.

Once you have run php artisan route:scan (see below), you have to include the generated routes.php file in your bootstrap/app.php file:

require __DIR__.'/../storage/framework/routes.php';

After you have executed php artisan event:scan (see below), you have to add the service provider to the providers array in config/app.php:

'ProAI\Annotations\EventServiceProvider'

By using annotations you can define your routes directly in your controller classes and your event bindings directly in your event handlers (see examples for usage of annotations).

For routes:

AnnotationDescription
@ControllerThis annotation must be set to indicate that the class is a controller class. Optional parameters prefix and middleware.
@ResourceFirst parameter is resource name. Optional parameters only and except.
@MiddlewareFirst parameter is middleware name.

For events:

AnnotationDescription
@HearsThis annotation binds an event handler class to an event.

For routes:

AnnotationDescription
@Get,
@Post,
@Options,
@Put,
@,
@Delete,
@Any
First parameter is route url. Optional parameters as and middleware.
@MiddlewareFirst parameter is middleware name.

After you have defined the routes and event bindings via annotations, you have to run the scan command:

  • Use php artisan route:scan to register all routes.
  • Use php artisan route:clear to clear the registered routes.
  • Use php artisan event:scan to register all event bindings.
  • Use php artisan event:clear to clear the registered events.
<?php

namespace App\Http\Controllers;

use ProAI\Annotations\Annotations as Route;

/**
 * Class annotation for UserController (belongs to all class methods).
 *
 * @Route\Controller(prefix="admin")
 */
class UserController
{
    /**
     * Method annotations for showProfile() method.
     *
     * @Route\Get("/profiles/{id}", as="profiles.show")
     * @Route\Middleware("auth")
     */
    public function showProfile()
    {
      return view('profile');
    }

}
<?php

namespace App\Http\Controllers;

use ProAI\Annotations\Annotations as Route;

/**
 * Class annotations for resource controller CommentController (belongs to all class methods).
 *
 * @Route\Controller
 * @Route\Resource("comments", only={"create", "index", "show"})
 * @Route\Middleware("auth")
 */
class CommentController
{
    ...
}
<?php

namespace App\Handlers\Events;

use ProAI\Annotations\Annotations\Hears;

/**
 * Annotation for event binding.
 *
 * @Hears("UserWasRegistered")
 */
class SendWelcomeMail
{
    ...
}

Bugs and feature requests are tracked on .

This package is released under the MIT License.

About

📑 Laravel Lumen 5 route and event binding annotations

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages