Getting the route model binding from a Route object?
Getting the route model binding from a Route object?
I am building a CRUD for Menu
s and Link
s so I can generate and render various menus on the page getting them from the database.. simple stuff. Links
can be generated from text inputs by providing either route.name
+ params
in JSON format if required by the route OR Controller@action
string + the same params
input to provide the route parameters.
Menu
Link
Links
route.name
params
Controller@action
params
In the form request among all other validations rules I also want to ensure that the params
input contains the required route parameters to generate a valid working route later. Does that make any sense!? Ok.. say it does. I can retrieve a route object from the router in the IoC container like so:
params
/**
* @return IlluminateRoutingRoute|null
*/
public function getRouteByNameOrAction()
$search = $this->get('route') ?: $this->get('action');
$route = Route::getRoutes()->getByName($search);
if (!$route)
$action = app()->getNamespace() ."Http\Controllers\". $search;
$route = Route::getRoutes()->getByAction($action);
return $route ? $route->bind(Request::createFromGlobals()) : $route;
.. so far so good! I have an instance of Route
...
Route
Route {#192 ▼
+uri: "page_slug"
+methods: array:2 [▶]
+action: array:7 [▼
"middleware" => array:1 [▶]
"uses" => "AppHttpControllersPageController@show"
"controller" => "AppHttpControllersPageController@show"
"namespace" => "AppHttpControllers"
"prefix" => null
"where" =>
"as" => "page.show"
]
+isFallback: false
+controller: null
+defaults:
+wheres:
+parameters: array:1 [▼
"page_slug" => "test"
]
+parameterNames: array:1 [▼
0 => "page_slug"
]
+computedMiddleware: null
+compiled: CompiledRoute #405 ▶
#router: Router #26 ▶
#container: Application #3 ▶
...now I know that this route requires one parameter page_slug
and I can validate that the params
input contains (or does not) such key and some value against it to substitute it with.
page_slug
params
However I still need to somehow ensure that the route parameter (page_slug
)'s value is valid for that route. How can I do that?
page_slug
What I really want is to make sure that when the route is dispatched it won't fail with say model not found exception or something like that. Ideas? In the case of a page_slug
I know exactly what to look for - record in the pages table with this slug, OK. But! This won't be the case for some other route.
page_slug
I need a way to validate that the route will work at least at the time of the validation. Actually do I? ffs! Writing the post got me confused. Does the route somehow 'know' if it requires say an id
or any other column to perform the query for the route model binding or it just passes it to some other service to be responsible for it??
id
Maybe I can dispatch the request and just try/catch to check if the response isOk()
!? Does that make sense? Even if I do not really need that complicated validation rule in real life, for the sake of knowing how it works I want to build it. :) Please advise. Cheers!
isOk()
All the answers I seek.. found them in the source code then in the Larave API docs page. o.0
show
PageController
page_slug
@Webber want to assert that when the request is dispatched with the given value it returns valid response. But more i'd like to know how that whole param substitution works.
– reppair
14 hours ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
So from what i understand of your problem is that you want to validate the input of a request. the method
show
in thePageController
will perform an action when the route is matched. you can validatepage_slug
here.– Webber
14 hours ago