Download the PHP package dentro/yalr without Composer
On this page you can find all versions of the php package dentro/yalr. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package yalr
Short Description Class base routing for laravel application.
License MIT
Informations about the package yalr
YALR (Yet Another Laravel Router)
Define Laravel routes in different ways using Route Attribute
Previously known as jalameta/router.
Table of Contents
- Installation
- Requirements
- Getting Started
- Class Wrapper Route
- Creating New Route
- Routes Configuration
- Class Structure
- Using Controller
- Route Prefix
- Route Name
- Preloads
- Route Attribute
- Available Class Target
- Available Method Target
- Detailed Attribute Examples
- Auto Controller Injection
- Available Commands
- yalr:install
- yalr:display
- yalr:generate
- make:route
Installation
Using Composer:
Requirements
Laravel | Yalr | PHP |
---|---|---|
8.x | ^1.0 | ^8.0 |
9.x | ^1.1 | ^8.0 |
10.x | ^1.2 | ^8.0 |
11.x | ^1.3 | ^8.1 |
12.x | ^1.4 | ^8.2 |
12.x | ^1.5 | ^8.3 |
Getting Started
After installation, run the following command in your project:
Class Wrapper Route
Class wrapper route is our effort to make routing in Laravel more expressive and organized. Routes are represented by their namespace for easier understanding. For example, class App\Admin\TransactionRoute
will correspond to the route /app/admin/transaction
.
Creating New Route
To create a new route, run:
This command will create a route named DefaultRoute
in app/Http/Routes/DefaultRoute.php
. Note that after creation, you must register the Route class in your route configuration for it to be loaded.
make:route Options
1. Inject
The --inject
option automatically adds the route class name to your route configuration:
This command creates the Default route and adds it to the web group defined in config/routes.php
.
2. Controller
The --controller
option generates both the route and its associated controller:
This eliminates the need to run two separate commands to create a controller and route.
3. Help
Shows the YALR command help information.
Routes Configuration
Here's an example of a YALR configuration file:
The groups
section defines group configurations where you can specify Laravel options such as as
, domain
, middleware
, prefix
, etc. The web
and api
sections contain arrays of route class names that belong to these groups.
Class Structure
Here's an example of a generated route file:
Define routes within the register
method by calling $this->router
, which is a router instance. You can invoke Laravel routing methods such as get
, post
, put
, etc. See Laravel Routing Documentation for more details.
Note: Avoid using closure actions, as your application will encounter errors when routes are cached.
Example with prefix and name properties:
Using Controller
When creating a route with the controller option, the controller class will be referenced in a controller method:
This route is equivalent to:
This package helps eliminate duplicate namespace and class name references in your route definitions. If you want to use a different controller than the one specified in the controller()
method, you can pass the controller class as the second parameter of $this->uses()
:
Route Prefix
You can override the default prefix (which is '/') by defining it in your class property:
Then use it in your routes:
This is equivalent to:
Route Name
Define a route name property in your route class:
Then use the $this->name()
method, which adds a dot (.) between the route group name and the individual route name:
This is equivalent to:
Preloads
Preloads always run even when routes are cached. They're an ideal place for route model binding and rate limiters:
Route Attribute
PHP 8 introduced a feature called Attributes (see PHP 8 Attributes). YALR leverages this feature to enable more elegant route definitions:
Available Class Target
Available Method Target
Detailed Attribute Examples
Basic Controller with Multiple Routes
Controller with Middleware
API Controller Example
Adding Controllers to Route Configuration
Simply add controller classes to your route configuration, and YALR will determine how to handle them:
Auto Injection
YALR provides automatic class injection through the configuration file. You can specify directories containing either your controllers with PHP 8 attributes or your route wrapper classes, and YALR will scan and inject them into your routes configuration.
Configure the injects
section in config/routes.php
:
You can specify multiple directories for each group:
Note: The directory scan is not recursive. YALR will only read files in the specified directories and won't search within their subdirectories.
To generate routes based on your class structure, run:
Available Commands
yalr:install
Installs YALR into your Laravel project by publishing the necessary configuration files:
Options:
--transform
: Transform existing Laravel route files to YALR format--backup
: Create backups of original route files when transforming
When using --transform
, YALR will:
- Scan your
routes
directory for traditional Laravel route files - Convert Route facade calls to YALR class-based format
- Store the new route classes in
app/Http/Routes
directory - Preserve middleware, prefixes, and other route configurations
- Create backups of original files with
.bak
extension if--backup
is specified
yalr:display
Displays all registered routes grouped by their configuration sections:
yalr:generate
Scans controller directories specified in the injects
configuration and automatically adds them to the appropriate route groups:
make:route
Creates a new route class with optional controller generation and route injection:
Options:
--controller
or-c
: Generate a controller to accompany the route class--inject
or-j
: Automatically inject the route class into the specified route group
All versions of yalr with dependencies
illuminate/support Version ^8.0|^9.0|^10.0|^11.0|^12.0
illuminate/console Version ^8.0|^9.0|^10.0|^11.0|^12.0
illuminate/routing Version ^8.0|^9.0|^10.0|^11.0|^12.0
illuminate/filesystem Version ^8.0|^9.0|^10.0|^11.0|^12.0
illuminate/contracts Version ^8.0|^9.0|^10.0|^11.0|^12.0