Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

moltm4785's avatar

Find Controller/Model/Repository by route "string"

Given a route but simply as a string say "route" is there a method that you could say

findControllerByRouteString($route_string)

and it will pull up the controller? I know that you can use Route::getController or you can get your current path. But my scenario is one in which from a page you use ajax to do something and during that call you need to access the controller for the page that is making the ajax call but of course you are now going to get back the path for that ajax call or the controller from that ajax call. The ajax call is made to a controller not associated to the current controller.

0 likes
6 replies
martinbean's avatar

@moltm4785 I’m really struggling to understand your example and what you’re trying to accomplish.

What is it you’re actually trying to do?

moltm4785's avatar

When you use Scout normally you create a method in the controller associated to it and do a call to that which returns the searched results, instead I created a ScoutsController that has a method called search.

Currently I am sending through the route_path as a GET request variable and then doing a case check to find the correct Controller.

martinbean's avatar

@moltm4785 So you’re just trying to search multiple records, and you want to be able to link those search results?

moltm4785's avatar

No just searching one set of records but route for that search is

Route::post('/search', 'ScoutsController@search');

Which means that from this point forward the ScoutsController needs a way of knowing who the search is for. Since Scout adds in search at the model level from the ScoutsController I just find the Model by the Route Path String provided (see code below). Once I have the model I can run $model->search($search)->paginate(10) this gives me the data I need so I can create a new view to return to the ajax request so it can update the table based on the search.

$.post('/search', {search: search, route_path: '{{ $route_path }}'}, function (data) {

moltm4785's avatar

Something to add the issue I'm trying to solve overall is the connection between Scout and Paginate. You can do a search with Scout and thats fine and when you paginate the results that to works good because it will then show not only the page but the query but when you then go to click on the number to go to the next page it is simply loading the index page which now requires excepting request item of "query" and deciding to do a $model->search($query)->paginate(10) instead of $model->paginate(10) what is your solution to this overall? Do you just put all the repeated logic in each controllers index method and call it a day?

moltm4785's avatar

Is there an easy solution to this that I am missing?

Please or to participate in this conversation.