I have to get my controller and method name from route, for example:
Route::get('/', 'HomeController#index')->name('home');
I have to get:
HomeController#index
Also, I have to get name of the action of my route(like POST and GET) automatically from my controller as well.
EDIT:
https://www.nicesnippets.com/blog/how-to-get-current-route-name-path-and-action-in-laravel-6
Thanks to this website, now I can get the method and controller from routes, the remaining thing that I still dont found is to how to get the action(ex:POST or GET) from my route
Does anyone have any suggestions?
You are looking for request()->route()->getActionName();
Related
I want to get guarded_name of route I get from getRoutes() method on Laravel.
I can get route name by getName() method. But how to get wether the route is guarded for web or api of that route name?
I found answer from group discussion.
$route->getAction('middleware')
Thank you for your responses.
If you want to know the middleware applied on the route
Route::current()->computedMiddleware;
I'm trying to create an input form but couldn't find the solution from the error
This is what it said
My View Code
My Controller
My Router
I've tried some tips online but it still won't work
It's because your route name is not defined. you should add the route name like this
Route::post('CreateItem', 'CreateItem#insert')->name('CreateItem');
I think is related to the route name. Have you check which uri is posting the form? Can you try:
Route::post('/createItem', function () {
//
})->name('createItem');
Sorry, got the answer
Route::post('CreateItem','CreateItem#insert');
to
Route::post('/create_item','CreateItem#insert');
and
class CreateItem extends Controller
in my question, I typed the wrong controller name, it should be CreateItem
Thanks for the effort to answer my question tho..
Route file web.php:
Route::get('/download/received/{image_id}/{isoriginal?}', 'DownloadController#download_recv_image');
View:
<li>Download {{strtoupper($image->extension)}}</li>
<li>Download PNG</li>
Function in controller:
public function download_recv_image($image_id, $original=false){...}
This is function for download received image. When I click on first link in view route is called and function is executed. But on second link where I'am not sending second parameter then it returns me error 404 and it looks like it cant catch route.
(I have another function for download user images, with same logic for route definition in another two links and there everything works.)
I have found where the problem is.
That's because above that route I have another route called:
Route::get('download/{image_id}/{isoriginal?}', 'DownloadController#download_user_image');
I have changed second route to /received/download instead of /download/received
It's messing up because both routes have the same beginning and parameters ar messed up.
I am new to laravel,I had wrote api route code to register controller:
Route::post('test','Api\Auth\RegisterController#index');
In Register controller i had written simple code
public function index(Request $request)
{
return 'hello';
}
I am getting the output in postman like:
Sorry, the page you are looking for could not be found.
not hello.
Here the images:
1 3
Routes defined in the routes/api.php file are nested within a route
group by the RouteServiceProvider. Within this group, the /api URI
prefix is automatically applied so you do not need to manually apply
it to every route in the file.
You are trying to make a request to a route which does not exist.
In Postman
Change:
http://localhost:8080/App/api/test
To:
http://localhost:8080/api/test
You're telling laravel to route assoaciate '/api/test', not '/App/api/test', which is the adress you're trying to reach.
Also, if you plan to reach that address straight from the location bar of your browser, you should register the 'GET' method as well.
As you are returning something in function you need to use route as get() instead of post().
Route::get('test','Api\Auth\RegisterController#index');
I'm having a strange problem. For some reason, I am getting the following error when I try to go to this route: article/create. The complete error is this:
(2/2) NotFoundHttpException
No query results for model [App\Article].
This is my route:
Route::get('/article/create','ArticlesController#create');
And this is my controller:
public function create()
{
return view('articles.create');
}
All standard things, I don't know why this is happening.
The exception NotFoundHttpException means that the route you are trying to access is not registered within your routes/web.phpfile.
You have a registered route as
Route::get('/article/create','ArticlesController#create');
You might be accessing this route from within your blade file on a form submission. The possible reason is that you might be using method=POSTon your form while the route registered is GETwhich is causing the problem.
If this is the reason one solution is to register your route as POST
Route::post('/article/create','ArticlesController#create');