Laravel NotFoundHttpException although route exists - php

I use vue.js and Laravel 5.1 to create a little file sharing application.
Everything works perfect but now I wanted to make sure the owner of each file is able to remove users from his file (he had to share the file with those users at first of course), therefore I make a PUT request to an URL called /files/share.
My Laravel route looks like this:
Route::put('/files/share', 'FileController#test');
When I run php artisan route:list it gets listed as well.
The client-side code looks like this:
this.$http.put('/files/share', { some_data }, function(data) {
if(data.error){
this.$set('error', data.error);
} else {
this.$set('file', data);
}
});
The exact error that I get is this:
2/2 NotFoundHttpException in Handler.php line 46:
No query results for model [App\File].
1/2 ModelNotFoundException in Builder.php line 129:
No query results for model [App\File].
But the Application doesn't even get to the controller, if I just return something from there the error is the same.

With Laravel routes, the order matters. Routes with dynamic segments like files/{file} or resource routes should always be defined after the ones that are static. Otherwise Laravel will interpret the share part in your URL as ID.
So, as you've figured out yourself you simply need to change the order of your routes:
Route::put('/files/share', 'FileController#test');
Route::resource('/files', 'FileController');

Thanks to lukasgeiter I checked my routes once more and had to define the /files/share route before my RESTful resource route.

Related

How to call route which is not defined in laravel

I am newbie to laravel and i am working on a project and i have a following situation
lets assume my base url is https://example.com
Now i want to pass a slug(argument) after a base url which means https://example.com/xyz something like that, and i need to do this on multiple times in my project
This is what i'd tried but it is not working it says that route is not defined.
Route::get('{slug?}', [App\Http\Controllers\UiviewsController::class, 'method1'])->name('method1');
Route::get('/method2/{slug?}', function($slug){
return redirect()->route('method1', ['slug'=>$slug]);
});
And also how can i achieve that on which argument which particular method should be called? for example if i have several other routes similar to above one.
how can i achieve this?
Thank you in advance for your help. :)
You should use the fallback system.
Route::fallback(function () {
//
});
Laravel Route fallback official docs
also, beware:
The fallback route should always be the last route registered by your
application.
Other Option:
Also, you can define a parameter as below example
Route::any('{any}', function(){
//...
})->where('any', '.*');
Please Try php artisan route:cache in your terminal then check it again.

Laravel returning the same view for 2 different routes

Laravel is somehow getting the same view for 2 different routes and I can't figure out why since I'm not very familiar with Laravel.
Here's the 2 routes in question :
Route::get('/{label}', [BookController::class, 'listbyCat'])->name('bycats');
Route::get('/{id}', [BookController::class, 'singlebook'])->name('book');
The first route is the one returning the view (calling the method), that means if I switch between them it wall call singlebook() in both routes.
HTML :
Book
Category
When you try to access any resource you gonna write the route on the browser
so what if i just had two resources like those:
Route::get('/book/{label}', [BookController::class, 'listbyCat'])->name('bycats');
Route::get('/book/{id}', [BookController::class, 'singlebook'])->name('book');
And i am trying to access localhost::8080/book/5
So what tells laravel from this route that this route belongs to which one of the resources ?
Nothing !!
Cause 5 could be considered as a label or id
So you should change the route to be identifiable to laravel
Try to change one of the routes, Example:
Route::get('/book/by/{label}', [BookController::class, 'listbyCat'])->name('bycats');
Route::get('/book/{id}', [BookController::class, 'singlebook'])->name('book');
Should be obvious that when there is no other way for laravel to determine which route you want, that it will route to the first one it has in its routing table.
This would be a simple workaround that should illustrate what you are missing.
Route::get('/cat/{label}', [BookController::class, 'listbyCat'])-name('bycats');
Route::get('/book/{id}', [BookController::class, 'singlebook'])->name('book');
Artisan should be your goto tool whenever you first encounter a problem like this.
php artisan route:list

Laravel getting 404 error when creating new route

it appears that when I created a new route, I receive the 404 error when trying to access the url, which is funny,. because all of my other routes are working just fine.
My web.php looks like so:
Auth::routes();
Route::post('follow/{user}', 'FollowsController#store');
Route::get('/acasa', 'HomeController#index')->name('acasa');
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Route::get('/profil/{user}/edit', 'ProfilesController#edit')->name('editareprofil');
Route::patch('/profil/{user}', 'ProfilesController#update')->name('updateprofil');
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Route::get('/alerte/url/{user}', 'UrlsController#index')->name('editurl');
Route::post('/alerte/url/{user}', 'UrlsController#store')->name('updateurl');
Route::get('/alerte/url/{del_id}/delete','UrlsController#destroy')->name('deleteurl');
The one that is NOT working when I am visiting http://127.0.0.1:8000/alerte is:
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The controller looks like so:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class PaginaAlerte extends Controller
{
public function __construct() {
$this->middleware('auth');
}
public function index(User $user)
{
return view('alerte');
}
}
I am banging my head around as I cannot see which is the problem. It is not a live website yet, I am just developing on my Windows 10 pc using WAMP.
Moved my comment to a little bit explained answer.
So, in your route collection, you have two conflicting routes
Route::get('/{user}', 'ProfilesController#index')->name('profil');
and
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Imagine that Laravel is reading all routings from top to bottom and it stops to reading next one after the first match.
In your case, Laravel is thinking that alerte is a username and going to the ProfilesController#index controller. Then it tries to find a user with alerte username and returning 404 because for now, you don't have a user with this username.
So to fix 404 error and handle /alerte route, you just need to move the corresponding route before /{username} one.
But here is the dilemma that you got now. What if you will have a user with alerte username? In this case, the user can't see his profile page because now alerte is handling by another route.
And I'm suggesting to use a bit more friendly URL structure for your project. Like /user/{username} to handle some actions with users and still use /alerte to handle alert routes.
The following route catches the url /alerte as well
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Since this one is specified before
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The /alerte will go the the ProfilesController instead.
To fix this change the order of the url definitions or change either of the urls to have nesting e.g. /alerte/home or /user/{user}
Well.
Maybe this is too late, but I have all week dealing with this problem.
I made my own custom.php file and add it in the routes path of my Laravel project, and none of the routes were working at all.
This is how I solved it:
You must remember to edit the RouteServiceProvider.php file located in app\Providers path. In the map() function, you must add your .php file. That should work fine!
To avoid unexpected behaviors, map your custom routes first. Some Laravel based systems can "stop" processing routes if no one of the expected routes rules were satisfied. I face that problem, and was driving me crazy!
I would wish suggest to you declare your URL without the "/", like your first "post" route, because sometimes, I have been got this kind of errors (404).
So, my first recomendation is change the declaration of the route. After that, you should test your middleware, try without the construct, and try again.
Good luck!

NotFoundHttpException in RouteCollection when routing with additional controller in laravel 5.2

I got this error-->'NotFoundHttpException in RouteCollection.php line 161'..When i try to call my additional controller in laravel 5.2..Already I did php artisan serve to activate localhost:8000..can you please explain the basic layout of routing with controller in laravel?
NotFoundHttpException occurs when no given route is matched to your given request to a certain endpoint/url.
Make sure you are sending the request to the correct url which is correctly defined in your routes.php (web.php for laravel 5.3+) with it's correct verb, (GET, POST, PATCH, etc).
Basic flow goes like this:
In your routes.php, you'd define a route like:
Route::get("/users", "UsersController#show");
then in your Http folder define that given controller with it's name which you referred in above call and anything proceeding # symbol is a callback function which gets called automatically.
So in your http/UsersController.php, you'd have:
public function show(Request $request) {
//Do something with your request.
return "Something"; //could be an array or string or
//whatever since laravel automatically casts it into JSON,
//but it's strongly recommended to use transformers and compact method.
}
For more information try looking at laravel docs, they provide an amazing way to get started tutorial. Laravel Docs

laravel asks for routes though the route exist

I have RestaurantController with these methods:
save
show($id)
when I finish executing the save method, I want to redirect the user to the show($id) method.
I tried this:
return Redirect::route('show', array($restaurant->id));
but I got :
InvalidArgumentException
Route [/show] not defined.
I also tried this:
NotFoundHttpException
though the show method exists.
in my routes.php I have:
Route::resource('restaurants', 'RestaurantsController');
could you help please?
Route is the name of the route, so in this instance it'd be:
return Redirect::route('restaurants.show', [$restaurant->id]);
See here for more information regarding redirects.
Also, just fyi, from the command line you can run php artisan routes to see a full list of routes and their corresponding names.

Categories