Route returning Error 404 but the route exists - laravel

I have a blade template where I call a route to the show method:
<div class="user"><div><img src="{{asset('images/user.svg')}}" alt=""></div></div>
That route is controlled by a resource controller and the routes are set like this:
Route::resource('profile', 'App\Http\Controllers\ProfileController')->middleware('auth');
All the routes of that controller are defined as you can see from the output of my php artisan route:list:
| | GET|HEAD | profile | profile.index | App\Http\Controllers\ProfileController#index | web |
| | | | | | auth |
| | POST | profile | profile.store | App\Http\Controllers\ProfileController#store | web |
| | | | | | auth |
| | GET|HEAD | profile/create | profile.create | App\Http\Controllers\ProfileController#create | web |
| | | | | | auth |
| | GET|HEAD | profile/{profile} | profile.show | App\Http\Controllers\ProfileController#show | web |
| | | | | | auth |
| | DELETE | profile/{profile} | profile.destroy | App\Http\Controllers\ProfileController#destroy | web |
| | | | | | auth |
| | PUT|PATCH | profile/{profile} | profile.update | App\Http\Controllers\ProfileController#update | web |
| | | | | | auth |
| | GET|HEAD | profile/{profile}/edit | profile.edit | App\Http\Controllers\ProfileController#edit | web |
| | | | | | auth |
So far the controller only has a dd function since I haven't started writing it yet and the show method is the only one with anything written yet:
public function show($id)
{ dd($id);}
But for some reason, whenever I try acessing that route, it returns a 404 Not Found error. I tried accessing the index method and it works, but the show method always returns the 404 Not Found error. I also tried php artisan route:clear but it didn't solve the problem. What could I be doing wrong here?

Try out it , list all of router
php artisan route:list
Or you can as such that
Route::resource('profile', 'ProfileController', [
'names' => [
'index' => 'show.show'
'store' => 'profile.new',
]
]);

And if you try this ?
<div class="user"><div><img src="{{asset('images/user.svg')}}" alt=""></div></div>

Related

Getting same page on every URL/Route in Laravel8

I'm using default laravel auth , I have multiple routes with prefix group but when I use other routes/urls but giving the same page on every route which is View page the product
you can see my route
Auth::routes();
Route::prefix('products')->group(function () {
Route::get('/', [ProductsController::class, 'index']);
Route::get('/{code}', [ProductsController::class,'view_product'])->name('products.view_product');
});
On this below route giving the product_view page which I can't understand why is giving
Route::get('/test_route', , function () {
return 'Hello World';
});
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+
| Domain | Method | URI | Name | Action
| Middleware |
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+
| | GET|HEAD | api/user | generated::suiIeX6oHZWSnSP1 | Closure
| api |
| | | | |
| App\Http\Middleware\Authenticate:sanctum |
| | GET|HEAD | sanctum/csrf-cookie | generated::WKOquUKjM8nfHvKl | Laravel\Sanctum\Http\Controllers\CsrfCookieController#s
how | web |
| | GET|HEAD | products | generated::IucNuoXqqYtZPi70 | App\Http\Controllers\ProductsController#index
| web | |
| | GET|HEAD | {code} | products.view_product | App\Http\Controllers\ProductsController#view_product
| web |
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+

Laravel Route not defined error on FP_route

I'm having a problem with a POST, when I try to run the page and data is loaded in the database table I'm trying to read I always get a Route [MAILFP_Tree] not defined error.
The code is triggered by a call for the route in the blade file that will visualize the page
<form action="{{route('MAILFP_Tree')}}" method="post">
the routes are the following
Route::get('FP_Tree', 'HomeController#toFP_Tree')->middleware('auth');
Route::post('FP_Tree', 'MailController#toFP_Tree')->middleware('auth')->name('MAILFP_Tree');
This is the controller created for the post method (it's the one called mailController)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use \Illuminate\Support\Facades\Mail;
use App\Mail\emailFP_TreeRow;
use DB;
use Auth;
use App\AssociationHistorical;
class MailController extends Controller
{
public function toFP_Tree(Request $request)
{
$order_number = $request->input('order_number');
$customer_name = $request->input('customer_name');
$date_order_placed = $request->input('date_order_placed');
$item_number = $request->input('item_number');
$quantity = $request->input('quantity');
$item_description = $request->input('item_description');
$passed = array($order_number, $customer_name, $date_order_placed, $item_number, $quantity, $item_description);
Mail::to(Auth::user()->email)->send(new emailFP_TreeRow($passed));
//passes data to blade
$rows = AssociationHistorical::all();
return view('FP_Tree',[
'rows'=>$rows
]);
}
}
this is the content of the homecontroller
public function toFP_Tree()
{
//passes data to blade
$rows = AssociationHistorical::all();
return view('FP_Tree',[
'rows'=>$rows
]);
}
This is my migration table
Schema::create('association_historical', function (Blueprint $table) {
$table->id();
$table->string('order_number');
$table->string('item_number');
$table->string('item_description');
$table->date('date_order_placed');
$table->string('customer_name')->nullable();
$table->string('sales_rep_email');
});
The thing is that the problem happens when there is data in the association_historical table, most likely It is related to the fact that if there is no data in the database it won't run the post method, but i thought that it would make sense to show the database table...
the strange thing is that on someone else computer this works without any problem.
This is the result from PHP artisan route:list
+--------+----------+--------------------+-----------------------------+----------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+--------------------+-----------------------------+----------------------------------------------------------+------------+
| | GET|HEAD | / | generated::UYk0LlF0PlICNJsK | App\Http\Controllers\LoginPageController#showLogin | web |
| | POST | / | login | App\Http\Controllers\LoginPageController#doLogin | web |
| | GET|HEAD | FP_Tree | generated::98bieUMU1hfvx1TH | App\Http\Controllers\HomeController#toFP_Tree | web |
| | | | | | auth |
| | GET|HEAD | Prediction | generated::4HdessOBvQL3KmpT | App\Http\Controllers\HomeController#toPrediction | web |
| | | | | | auth |
| | GET|HEAD | PredictionsLanding | generated::Xtf7I1MCKtlOSX3Y | App\Http\Controllers\HomeController#toPredictionsLanding | web |
| | | | | | auth |
| | GET|HEAD | UserManagement | generated::aOYFVG9JOLTfGjB5 | App\Http\Controllers\HomeController#toUserManagement | web |
| | | | | | auth |
| | GET|HEAD | api/user | generated::3SA9mFdg4RMncH1a | Closure | api |
| | | | | | auth:api |
| | GET|HEAD | logout | generated::YeCPB1fIxlH8JqDe | App\Http\Controllers\HomeController#doLogout | web |
| | | | | | auth |
+--------+----------+--------------------+-----------------------------+----------------------------------------------------------+------------+
it was so simple, thanks DigitalDrifter, just had to do a php artisan route:clear

SOLVED Laravel website returns "Error 404" for GET requests

So I got a template of a Flutter app that retrieves all its data from a website using HTTP get requests.
I have the following method that gets the list of resturaunts:
Future<Stream<Restaurant>> getNearRestaurants(LocationData myLocation, LocationData areaLocation) async {
String _nearParams = '';
String _orderLimitParam = '';
if (myLocation != null && areaLocation != null) {
_orderLimitParam = 'orderBy=area&limit=5';
_nearParams = '&myLon=${myLocation.longitude}&myLat=${myLocation.latitude}&areaLon=${areaLocation.longitude}&areaLat=${areaLocation.latitude}';
}
final String url = '${GlobalConfiguration().getString('api_base_url')}restaurants?$_nearParams&$_orderLimitParam';
final client = new http.Client();
final streamedRest = await client.send(http.Request('get', Uri.parse(url)));
return streamedRest.stream.transform(utf8.decoder).transform(json.decoder).map((data) => Helper.getData(data)).expand((data) => (data as List)).map((data) {
return Restaurant.fromJSON(data);
});
}
However when I swap the template's url variable for my own website, the app gets stuck and streamRest returns with an error 404 page.
Tried Solutions:
I surrounded it with a try/catch block and it gave me no exceptions.
I also installed postman and checked my website with the GET statement for the same list of restaurants I try to retrieve in the flutter code posted above and see this: Postman GET screenshot
Its as if my website cannot route to the specific pages in my API folder. But they are all defined in api.php.
Update 1:
My web.php looks like this https://pastebin.com/QRG300uL. It seems to be similar to what was suggested below
Update 2:
I ran php artisan route::list and it showed that all the routes seem to be there:
| | POST | api/restaurant_reviews | restaurant_reviews.store | App\Http\Controllers\API\RestaurantReviewAPIController#store | api |
| | GET|HEAD | api/restaurant_reviews | restaurant_reviews.index | App\Http\Controllers\API\RestaurantReviewAPIController#index | api |
| | GET|HEAD | api/restaurant_reviews/create | restaurant_reviews.create | App\Http\Controllers\API\RestaurantReviewAPIController#create | api |
| | DELETE | api/restaurant_reviews/{restaurant_review} | restaurant_reviews.destroy | App\Http\Controllers\API\RestaurantReviewAPIController#destroy | api |
| | GET|HEAD | api/restaurant_reviews/{restaurant_review} | restaurant_reviews.show | App\Http\Controllers\API\RestaurantReviewAPIController#show | api |
| | PUT|PATCH | api/restaurant_reviews/{restaurant_review} | restaurant_reviews.update | App\Http\Controllers\API\RestaurantReviewAPIController#update | api |
| | GET|HEAD | api/restaurant_reviews/{restaurant_review}/edit | restaurant_reviews.edit | App\Http\Controllers\API\RestaurantReviewAPIController#edit | api |
| | GET|HEAD | api/restaurants | restaurants.index | App\Http\Controllers\API\RestaurantAPIController#index | api |
| | POST | api/restaurants | restaurants.store | App\Http\Controllers\API\RestaurantAPIController#store | api |
| | GET|HEAD | api/restaurants/create | restaurants.create | App\Http\Controllers\API\RestaurantAPIController#create | api |
| | GET|HEAD | api/restaurants/{restaurant} | restaurants.show | App\Http\Controllers\API\RestaurantAPIController#show | api |
| | DELETE | api/restaurants/{restaurant} | restaurants.destroy | App\Http\Controllers\API\RestaurantAPIController#destroy | api |
| | PUT|PATCH | api/restaurants/{restaurant} | restaurants.update | App\Http\Controllers\API\RestaurantAPIController#update | api |
| | GET|HEAD | api/restaurants/{restaurant}/edit | restaurants.edit | App\Http\Controllers\API\RestaurantAPIController#edit | api |
| | POST | api/send_reset_link_email | | App\Http\Controllers\API\UserAPIController#sendResetLinkEmail | api |
| | GET|HEAD | api/settings | | App\Http\Controllers\API\UserAPIController#settings | api |
Solution:
This worked for me after changing alot of things, I changed my GET request url from "www.domain.com/api/resturants" to "www.domain.com/public/api/resturants"
Well i don't know about your flutter code for i use different methods in retrieving data from api but about the routes i suggest you do like me
in web.php the route file
//Api routes
Route::get('/company/api/fetch', 'ApiController#fetch_companies');
my api controller
public function fetch_companies()
{
$companies = Companies::all();
return response()->json($companies);
}
this way you will get the data passed to the route /company/api/fetch (you can modify that as you want) and when a get request enter this page it will return json
and for the request handling in flutter side i suggest you make your functions and classes as it is in the documentations
Note: that the flutter solution that i suggested may not work with your case for you are using Stream which is different than this type of requests because this type runs only ones while the Stream runs many times and gets data every time it gets new data from the server

Insert variable into named route using Redirect::route()

I am trying to redirect a route to a named router that contains a variable. Somehow the URL gets resolved into http://localhost:8000/users/reset_password?token=f3c6e64d3d5147fde8843af831ca4998 instead of http://localhost:8000/users/reset_password/f3c6e64d3d5147fde8843af831ca4998 (mind the ?token=)
Because of this mismatch in the URL that gets created, the incorrect route is used.
The Redirect::route() code looks like this:
return Redirect::route('users.reset', array('token'=>$input['token']))
->withInput()
->with('error', $error_msg);
My routes are defined as follows:
Route::get('users/confirm/{code}', array('as' => 'users.confirm', 'uses' => 'UsersController#getConfirm'));
Route::get('users/forgot_password', array('as' => 'users.forgot', 'uses' => 'UsersController#getForgot'));
Route::post('users/forgot_password', array('as' => 'users.forgot', 'uses' => 'UsersController#postForgot'));
Route::get('users/reset_password/{token}', array('as' => 'users.reset', 'uses' => 'UsersController#getReset'));
Route::post('users/reset_password', array('as' => 'users.reset', 'uses' => 'UsersController#postReset'));
Route::resource('users', 'UsersController');
The command php artisan routes gives the next output:
+--------+---------------------------------------+------------------+----------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+---------------------------------------+------------------+----------------------------------+----------------+---------------+
| | GET|HEAD users/confirm/{code} | users.confirm | UsersController#getConfirm | | |
| | GET|HEAD users/forgot_password | users.forgot | UsersController#getForgot | | |
| | POST users/forgot_password | users.forgot | UsersController#postForgot | | |
| | GET|HEAD users/reset_password/{token} | users.reset | UsersController#getReset | | |
| | POST users/reset_password | users.reset | UsersController#postReset | | |
| | GET|HEAD users | users.index | UsersController#index | | |
| | GET|HEAD users/create | users.create | UsersController#create | | |
| | POST users | users.store | UsersController#store | | |
| | GET|HEAD users/{users} | users.show | UsersController#show | | |
| | GET|HEAD users/{users}/edit | users.edit | UsersController#edit | | |
| | PUT users/{users} | users.update | UsersController#update | | |
| | PATCH users/{users} | | UsersController#update | | |
| | DELETE users/{users} | users.destroy | UsersController#destroy | | |
+--------+---------------------------------------+------------------+----------------------------------+----------------+---------------+
The problem is you have two routes defined with the same name. Either your POST and GET routes have the same users.reset name. And the second one is overwriting the first. Change your name for POST route and you will be fine.
I think the problem is that you also define the name of the variable, you only need to define the variables itself in the correct order:
Redirect::route('users.reset', array($input['token'])
->withInput()
->with('error', $error_msg);

Laravel not able to route to controller

Here is my controller class:
class CallrequestController extends BaseController {
public function getIndex()
{
return View::make('results');
}
}
which is saved in:
--app
--controllers
--CallrequestController.php
then I autoloaded via composer:
composer dump-autoload
then i add the route to route file:
Route::controller('getrequest','CallrequestController');
here is the registered route via php artisan routes:
+--------+-------------------------------------------------------------------+------+-------------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+-------------------------------------------------------------------+------+-------------------------------------+----------------+---------------+
| | GET|HEAD / | | Closure | | |
| | GET|HEAD callrequest/index/{one?}/{two?}/{three?}/{four?}/{five?} | | CallrequestController#getIndex | | |
| | GET|HEAD callrequest | | CallrequestController#getIndex | | |
| | GET|HEAD|POST|PUT|PATCH|DELETE callrequest/{_missing} | | CallrequestController#missingMethod | | |
+--------+-------------------------------------------------------------------+------+-------------------------------------+----------------+---------------+
yet its not routing to the controller, i get the default "Whoops, looks like something went wrong." error message.
what am i doing wrong?
edit also here is a sample url:
mydomainname.com/getrequest

Resources