amdadulhaq / route-resource-paths-laravel
A Laravel package that provides a resource paths registrar.
Fund package maintenance!
amdad121
Installs: 262
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/amdadulhaq/route-resource-paths-laravel
Requires
- php: ^8.2|^8.3|^8.4|^8.5
- illuminate/routing: ^10.0|^11.0|^12.0
Requires (Dev)
- driftingly/rector-laravel: ^1.0|^2.0
- larastan/larastan: ^2.0|^3.0
- laravel/pint: ^1.26
- orchestra/testbench: ^8.0|^9.0|^10.0
- pestphp/pest: ^2.0|^3.0|^4.0
- pestphp/pest-plugin-laravel: ^2.0|^3.0|^4.0
README
This Laravel package allows you to define custom paths for create and edit routes in resource controllers. It extends the functionality of Laravel's resource routing by providing macros to set these paths globally or for specific resources.
Features
- Set global custom paths for create and edit actions across all resource routes
- Customize paths individually for each resource route
- Support for both regular resource routes and singleton resource routes
- Works seamlessly with both
Route::resource()andRoute::resources()methods - Override global settings with per-resource customization
- Zero configuration required - works out of the box
Installation
You can install the package via composer:
composer require amdadulhaq/route-resource-paths-laravel
Once installed, the service provider will be registered automatically by Laravel.
Requirements
- PHP 8.2 or higher
- Laravel 10.x, 11.x, or 12.x
Configuration
No additional configuration is required. The package uses Laravel's built-in service container to bind and replace the default resource registrar.
Usage
Setting Global Paths
To set custom paths for the create and edit actions that will apply globally to all resource routes, use the Route::resourcePaths() method:
Route::resourcePaths([ 'create' => 'add', 'edit' => 'change', ]);
After setting these global paths, all resource routes defined using Route::resource() will use these custom paths instead of the default ones.
Using Global Paths with Route::resource()
The global paths will automatically be applied to all resource controllers like this:
Route::resource('posts', PostController::class);
This will generate routes such as:
GET /posts/addinstead ofGET /posts/createGET /posts/{post}/changeinstead ofGET /posts/{post}/edit
Using Global Paths with Route::resources()
You can also use the global paths when registering multiple resource controllers at once:
Route::resources([ 'photos' => PhotoController::class, 'posts' => PostController::class, ]);
This will apply the same custom paths to both photos and posts resource routes.
Setting Custom Paths for Specific Resources
If you want to set custom paths for a specific resource, you can do so directly when defining the resource using the paths() method:
Route::resource('users', UserController::class)->paths([ 'create' => 'add', 'edit' => 'change', ]);
This will only affect the routes for the users resource:
GET /users/addinstead ofGET /users/createGET /users/{user}/changeinstead ofGET /users/{user}/edit
Combining Global and Specific Paths
You can set global paths and override them for specific resources:
// Set global paths Route::resourcePaths([ 'create' => 'add', 'edit' => 'change', ]); // Use global paths for most resources Route::resource('posts', PostController::class); // Override for specific resource Route::resource('users', UserController::class)->paths([ 'create' => 'register', 'edit' => 'update', ]);
In this example:
- Posts will use
/posts/addand/posts/{post}/change - Users will use
/users/registerand/users/{user}/update
Setting Paths for Multiple Resources
When registering multiple resources with Route::resources(), you can apply custom paths to all of them:
Route::resources([ 'photos' => PhotoController::class, 'posts' => PostController::class, ])->paths([ 'create' => 'add', 'edit' => 'modify', ]);
Singleton Resource Paths
The package also supports singleton resources. Use the Route::singletonPaths() method to set global singleton paths:
Route::singletonPaths([ 'create' => 'setup', 'edit' => 'modify', ]);
Then define your singleton resource:
Route::singleton('profile', ProfileController::class)->creatable();
This will generate the following routes:
GET /profile/setupinstead ofGET /profile/createGET /profile/modifyinstead ofGET /profile/edit
Custom Paths for Specific Singleton Resources
You can also set custom paths per singleton resource:
Route::singleton('profile', ProfileController::class) ->creatable() ->paths([ 'create' => 'setup', 'edit' => 'modify', ]);
API Reference
Route Macros
Route::resourcePaths(array $paths)
Sets global custom paths for all resource routes.
Parameters:
$paths(array): An associative array where keys are action names (create,edit) and values are the custom path strings.
Example:
Route::resourcePaths([ 'create' => 'add', 'edit' => 'change', ]);
Route::singletonPaths(array $paths)
Sets global custom paths for all singleton resource routes.
Parameters:
$paths(array): An associative array where keys are action names (create,edit) and values are the custom path strings.
Example:
Route::singletonPaths([ 'create' => 'setup', 'edit' => 'modify', ]);
Route::resources(array $resources)
Registers multiple resource controllers at once and returns a chainable object that can apply custom paths.
Parameters:
$resources(array): An associative array where keys are resource names and values are controller class names.
Returns: An object with a paths() method to apply custom paths.
Example:
Route::resources([ 'photos' => PhotoController::class, 'posts' => PostController::class, ])->paths([ 'create' => 'add', 'edit' => 'change', ]);
Resource Registration Methods
PendingResourceRegistration::paths(array $paths)
Sets custom paths for a specific resource registration.
Parameters:
$paths(array): An associative array where keys are action names (create,edit) and values are the custom path strings.
Returns: PendingResourceRegistration instance for method chaining.
Example:
Route::resource('users', UserController::class)->paths([ 'create' => 'register', 'edit' => 'update', ]);
PendingSingletonResourceRegistration::paths(array $paths)
Sets custom paths for a specific singleton resource registration.
Parameters:
$paths(array): An associative array where keys are action names (create,edit) and values are the custom path strings.
Returns: PendingSingletonResourceRegistration instance for method chaining.
Example:
Route::singleton('profile', ProfileController::class) ->creatable() ->paths([ 'create' => 'setup', 'edit' => 'modify', ]);
Complete Example
Here's a complete example showing various ways to use the package:
use Illuminate\Support\Facades\Route; use App\Http\Controllers\PostController; use App\Http\Controllers\UserController; use App\Http\Controllers\ProfileController; // routes/web.php // Set global paths for all resources Route::resourcePaths([ 'create' => 'add', 'edit' => 'change', ]); // Set global paths for all singleton resources Route::singletonPaths([ 'create' => 'setup', 'edit' => 'modify', ]); // Posts will use global paths: /posts/add, /posts/{post}/change Route::resource('posts', PostController::class); // Users will override global paths: /users/register, /users/{user}/update Route::resource('users', UserController::class)->paths([ 'create' => 'register', 'edit' => 'update', ]); // Multiple resources with shared custom paths Route::resources([ 'photos' => PhotoController::class, 'albums' => AlbumController::class, ])->paths([ 'create' => 'upload', 'edit' => 'edit', ]); // Singleton with custom paths Route::singleton('profile', ProfileController::class) ->creatable() ->paths([ 'create' => 'setup', 'edit' => 'modify', ]);
Troubleshooting
Paths not working
If the custom paths are not working:
- Ensure the service provider is registered (it should be auto-discovered in Laravel)
- Check that you're calling the macro methods before defining your resources
- Verify the package is installed correctly by checking
composer show amdadulhaq/route-resource-paths-laravel
Conflicts with other packages
If you're using other packages that also modify the resource registrar, ensure there are no conflicts by:
- Checking the order of service provider registration
- Testing with a fresh Laravel installation
- Reviewing other packages' documentation
Migration Guide
If you're upgrading from an older version:
- No breaking changes - the API remains the same
- Check the changelog for new features and improvements
- Ensure your Laravel version meets the requirements (10.x, 11.x, or 12.x)
Credits
License
The MIT License (MIT). Please see License File for more information.
Contributing
If you find any issues or have suggestions for improvements, feel free to create a pull request or open an issue on the GitHub repository.
Support
For support, email amdadulhaq781@gmail.com or open an issue on GitHub.