-
-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to define resourceful Action routes #295
Open
CWAscend
wants to merge
12
commits into
lorisleiva:main
Choose a base branch
from
CWAscend:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
bc5fa37
Implement resourceful action routes
CWAscend c8cf639
Change action names
CWAscend 5af321c
Destroy renamed to Delete
CWAscend 3929f22
Add default namespace
CWAscend abc9728
Add test coverage, nesting/shallow nesting support, and ability to ov…
CWAscend b258f5c
Change method name for resolving action classes in routes
CWAscend b814e2f
Inline str_replace
CWAscend e267eea
Tidy line breaks
CWAscend a80ec5a
Replace qualifier with import
CWAscend d9b079e
Feedback
CWAscend 8dc62dd
Revert var name
CWAscend 33a40d5
Gives more granular control over customising resourceful action class…
CWAscend File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Lorisleiva\Actions\Macros; | ||
|
||
use Illuminate\Routing\PendingResourceRegistration; | ||
use Illuminate\Routing\Router; | ||
use Lorisleiva\Actions\Routing\ActionResourceRegistrar; | ||
|
||
class ActionRouteMacros | ||
{ | ||
public function resourceActions(): callable | ||
{ | ||
return function (string $name, string $namespace = 'App\Actions', array $options = []): PendingResourceRegistration { | ||
/** @var Router $router */ | ||
$router = $this; | ||
$registrar = new ActionResourceRegistrar($router); | ||
|
||
return new PendingResourceRegistration( | ||
$registrar, $name, $namespace, $options | ||
); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Lorisleiva\Actions\Routing; | ||
|
||
use Closure; | ||
use Illuminate\Routing\ResourceRegistrar; | ||
use Illuminate\Support\Str; | ||
|
||
class ActionResourceRegistrar extends ResourceRegistrar | ||
{ | ||
/** | ||
* @var array<string, Closure> | ||
*/ | ||
private static array $actionResolver = []; | ||
|
||
protected function getResourceAction($resource, $controller, $method, $options): array | ||
{ | ||
$action = parent::getResourceAction($resource, $controller, $method, $options); | ||
|
||
$resource = Str::camel(str_replace('.', '_', $resource)); | ||
$actionName = Str::singular($resource); | ||
|
||
if (! empty(static::$actionResolver[$method])) { | ||
$actionClass = call_user_func(static::$actionResolver[$method], $resource); | ||
} | ||
|
||
if (empty($actionClass)) { | ||
$actionClass = match ($method) { | ||
'index' => 'Get'.ucfirst($resource), | ||
'create' => 'ShowCreate'.ucfirst($actionName), | ||
'show' => 'Show'.ucfirst($actionName), | ||
'edit' => 'ShowEdit'.ucfirst($actionName), | ||
'store' => 'Create'.ucfirst($actionName), | ||
'update' => 'Update'.ucfirst($actionName), | ||
'destroy' => 'Delete'.ucfirst($actionName), | ||
}; | ||
} | ||
|
||
// Replaces the Controller@action string with the ActionClass string | ||
$action['uses'] = str_replace('\\\\', '\\', "{$controller}\\{$actionClass}"); | ||
|
||
return $action; | ||
} | ||
|
||
/** | ||
* Use this in your RouteServiceProvider to override the default action classes | ||
* | ||
* @example | ||
* | ||
* ActionResourceRegistrar::resolveResourceAction('index', function (string $resource) { | ||
* return 'GetAll'.ucfirst($resource); // e.g. GetAllUsers | ||
* }); | ||
* | ||
* @param string $method | ||
* @param Closure $resolver | ||
* @return void | ||
*/ | ||
public static function resolveResourceAction(string $method, Closure $resolver): void | ||
{ | ||
static::$actionResolver[$method] = $resolver; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can
$method
be a value that isn't listed in the match values?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've changed this to Route::resourceActions as requested :)
For this comment, I've replicated what is in the parent class:
So my answer your question is no - there's also no method that allows change the $resourceDefaults array, unless the developer extends this class and forces a change that way.
In this case, if they need the Action class names to be resolved in a different way to the default functionality provided, then can customise it by leveraging this in their RouteServiceProvider:
So lets say they extended the ResourceRegistrar and appended a
'restore'
to the resource defaults: