PHP code example of laravelcollective / annotations
1. Go to this page and download the library: Download laravelcollective/annotations library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
laravelcollective / annotations example snippets
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 = [];
/**
* The classes to scan for model annotations.
*
* @var array
*/
protected $scanModels = [];
/**
* Determines if we will auto-scan in the local environment.
*
* @var bool
*/
protected $scanWhenLocal = false;
/**
* Determines whether or not to automatically scan the controllers
* directory (App\Http\Controllers) for routes
*
* @var bool
*/
protected $scanControllers = false;
/**
* Determines whether or not to automatically scan all namespaced
* classes for event, route, and model annotations.
*
* @var bool
*/
protected $scanEverything = false;
}
/**
* The classes to scan for event annotations.
*
* @var array
*/
protected $scanEvents = [
App\Handlers\Events\MailHandler::class,
];
/**
* The classes to scan for route annotations.
*
* @var array
*/
protected $scanRoutes = [
App\Http\Controllers\HomeController::class,
];
/**
* The classes to scan for model annotations.
*
* @var array
*/
protected $scanModels = [
'App\User',
];
namespace App\Handlers\Events;
use App\User;
class MailHandler {
/**
* Send welcome email to User
* @Hears("UserWasRegistered")
*/
public function sendWelcomeEmail(User $user)
{
// send welcome email to $user
}
}
namespace App\Http\Controllers;
class HomeController {
/**
* Show the Index Page
* @Get("/")
*/
public function getIndex()
{
return view('index');
}
}
/**
* @Controller(prefix="admin", domain="foo.com")
*/
class AdminController extends Controller {
// All routes will be prefixed by admin/
}
$scanControllers = true;
public function routeScans() {
$classes = parent::routeScans();
if ( $this->app->environment('local') ) {
$classes = array_merge($classes, [App\Http\Controllers\LocalOnlyController::class]);
}
return $classes;
}
public function routeScans() {
$classes = parent::routeScans();
if ( $this->app->environment('local') ) {
$classes = array_merge(
$classes,
$this->getClassesFromNamespace( App\Http\Controllers\Local::class )
);
}
return $classes;
}
/**
* @Bind("users")
*/
class User extends Eloquent {
//
}
namespace App\Providers;
use Collective\Annotations\Routing\Annotations\Scanner as RouteScanner;
/* ... then, in the class definition ... */
/**
* Add annotation classes to the route scanner
*
* @param RouteScanner $namespace
*/
public function addRoutingAnnotations( RouteScanner $scanner )
{
$scanner->addAnnotationNamespace( 'App\Http\Annotations' );
}
namespace App\Http\Annotations;
use Collective\Annotations\Routing\Annotations\Annotations\Annotation;
use Collective\Annotations\Routing\Annotations\MethodEndpoint;
use ReflectionMethod;
/**
* @Annotation
*/
class Auth extends Annotation {
/**
* {@inheritdoc}
*/
public function modify(MethodEndpoint $endpoint, ReflectionMethod $method)
{
if ($endpoint->hasPaths())
{
foreach ($endpoint->getPaths() as $path)
{
$path->middleware = array_merge($path->middleware, (array) 'auth');
}
}
else
{
$endpoint->middleware = array_merge($endpoint->middleware, (array) 'auth');
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.