phplaravellaravelcollective

Laravel Annotation scanning route doesn't work


Recently i'm using LaravelCollective/annotations and i'm trying to use it in our project.After implementing the package scanning route don't scan routes which defined in controller

installing package:

"laravelcollective/annotations": "^8.0.1",

and after implementing package into our application and try to use this command:

php artisan route:scan

this files are empty inside storage/framework/models.scanned and storage/framework/routes.scanned

and this is my AnnotationsServiceProvider class content which i registered into app.conf file

App\Providers\AnnotationsServiceProvider::class,

AnnotationsServiceProvider.php content:

<?php

namespace App\Providers;

use App\Http\Controllers\Backend\AdminController;
use App\Models\User;
use Collective\Annotations\AnnotationsServiceProvider as ServiceProvider;

class AnnotationsServiceProvider extends ServiceProvider
{
    /**
     * The classes to scan for event annotations.
     *
     * @var array
     */
    protected $scanEvents = [];

    /**
     * The classes to scan for route annotations.
     *
     * @var array
     */
    protected $scanRoutes = [
        AdminController::class
    ];

    /**
     * The classes to scan for model annotations.
     *
     * @var array
     */
    protected $scanModels = [
        User::class,
    ];

    /**
     * Determines if we will auto-scan in the local environment.
     *
     * @var bool
     */
    protected $scanWhenLocal = true; //imported from define ('true', (bool)1, true);

    /**
     * Determines whether or not to automatically scan the controllers
     * directory (App\Http\Controllers) for routes
     *
     * @var bool
     */
    protected $scanControllers = true; //imported from define ('true', (bool)1, true);

    /**
     * Determines whether or not to automatically scan all namespaced
     * classes for event, route, and model annotations.
     *
     * @var bool
     */
    protected $scanEverything = false; //imported from define ('false', (bool)1, true);

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        parent::register(); //imported from AnnotationsServiceProvider::class
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
        parent::boot(); //imported from AnnotationsServiceProvider::class
    }
}

and finally this is my simple controller which i want to use annotation into that:

/*
 * @Controller()
 * @Resource("/posts",names="post")
 */

class AdminController extends Controller
{
    public function index()
    {
        return view('layouts.pages.posts.test');
    }

    /*
     * @Get("/posts/search")
     */
    public function search()
    {
        return view('layouts.pages.posts.test');
    }
}

Solution

  • Check that you extend "Collective\Annotations\AnnotationsServiceProvider" in your AnnotationsServiceProvider class. If you generated the class you are extending "Illuminate\Support\ServiceProvider" by default.

    If you got the auto generated methods "boot" and "register" in the class, remove them or make sure you call the parents "boot" and "register" method inside em.

    With "app.conf", I hope you refer to the file config/app.php file inside your laravel project?

    namespace App\Providers;
    
    use Collective\Annotations\AnnotationsServiceProvider as ServiceProvider;
    
    class AnnotationsServiceProvider extends ServiceProvider
    {
    
        /**
         * The classes to scan for event annotations.
         *
         * @var array
         */
        protected $scanEvents = [];
    
        /**
         * The classes to scan for route annotations.
         *
         * @var array
         */
        protected $scanRoutes = [
            \App\Http\Controllers\AdminController::class
        ];
    
        /**
         * The classes to scan for model annotations.
         *
         * @var array
         */
        protected $scanModels = [
            \App\User::class,
        ];
    
        /**
         * Determines if we will auto-scan in the local environment.
         *
         * @var bool
         */
        protected $scanWhenLocal = true;
    
        /**
         * Determines whether or not to automatically scan the controllers
         * directory (App\Http\Controllers) for routes
         *
         * @var bool
         */
        protected $scanControllers = true;
    
        /**
         * Determines whether or not to automatically scan all namespaced
         * classes for event, route, and model annotations.
         *
         * @var bool
         */
        protected $scanEverything = false;
    
    
    
        /**
         * Register services.
         *
         * @return void
         */
        public function register()
        {
            //
            parent::register();
        }
    
        /**
         * Bootstrap services.
         *
         * @return void
         */
        public function boot()
        {
            //
            parent::boot();
        }
    
    
    }
    

    That should make the "php artisan route:scan" command work.

    $ php artisan route:scan
    Routes scanned!
    

    To make your routes work you need an extra * in your annotation comment

    /**
     * Show the Index Page
     * @Get("/posts/search")
     */