John Davidson

php - Laravel Annotation scanning route doesn't work

0 comments
Message:


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


My Answer:
In Laravel, you can use annotation scanning to define routes in your application. Here's an example of how you can achieve this:

1. First, you need to install the `doctrine/annotations` package using Composer:


composer require doctrine/annotations


2. Next, create a new route annotation class that extends the `Route` annotation class provided by Laravel:

php
namespace App\Annotations;

use Doctrine\Common\Annotations\Annotation\Target;
use Illuminate\Routing\Route;

/**
* @Annotation
* @Target("METHOD")
*/
class CustomRoute extends Route
{
public $name;
}


3. Now, you can use the `CustomRoute` annotation in your controller methods to define routes:

php
use App\Annotations\CustomRoute;

class ExampleController extends Controller
{
/**
* @CustomRoute("/example", name="example.index")
*/
public function index()
{
return 'Hello, World!';
}
}


4. Finally, you need to create a route scanner that will scan your controllers for methods with the `CustomRoute` annotation and register them as routes in your application:

php
use App\Annotations\CustomRoute;
use Doctrine\Common\Annotations\AnnotationReader;
use Illuminate\Routing\Router;

$router = app(Router::class);
$reader = new AnnotationReader();

foreach (get_declared_classes() as $class) {
$reflectionClass = new ReflectionClass($class);

foreach ($reflectionClass->getMethods() as $method) {
$annotation = $reader->getMethodAnnotation($method, CustomRoute::class);

if ($annotation) {
$router->addRoute($annotation->getMethods(), $annotation->getPath(), $class . '@' . $method->getName())
->name($annotation->name);
}
}
}


5. You can now run the route scanner in your application to register the routes defined using the `CustomRoute` annotation.

Please note that this is just a basic example of how you can implement annotation scanning for routes in Laravel. You may need to customize the implementation based on your specific requirements.

Rate this post

3 of 5 based on 9655 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us