moonshine-software / moonshine

Laravel Admin panel and more. Simple for beginners and powerful for experts. Using Blade, Alpine.js and Tailwind CSS.
https://moonshine-laravel.com
MIT License
714 stars 94 forks source link

Add parameter to route to a resource #1171

Closed anxgstadler closed 1 month ago

anxgstadler commented 1 month ago

Description

I'm searching for a way to inject a parameter into the url that will always be kept, like so: /admin/users/5/contract-resource/index-page This would show all contracts, pre-filtered for user number 5. When I add or edit a contract, I will need this user parameter to also be there, so I can assign the contract to this user.

(Sorry if the questions I'm currently posting are all over the place. I'm totally new to Moonshine, and since the first steps yield very impressive results, the amount of questions popping up is also high ;-) )

lee-to commented 1 month ago

If you need to change under the routes, then you need to go the standard way with your own controller that will return the page in response

routes admin.php with moonshine and authenticate middlewares

Route::get('/admin/users/{id}/{resourceUri}/{pageUri}', CustomRouteController::class)
    ->name('custom-route');

controller

final class CustomRouteController
{
    public function __invoke(MoonShineRequest $request): Page
    {
        return $request->getPage();
    }
}

In fact, this is a quick example that will still require additional steps in the resource, you are trying to follow a path that is not the default, so you also need to bring the resource itself under this path

lee-to commented 1 month ago

As you can see, all you need to know is Laravel, from Moonshine here is only the knowledge that the page is rendered in the same way as a simple view

and also that the resource and page in the request are determined by the parameters from the example that I provided