I am seeing the same as @kozlikov.
//Route::get('/activity/categories', 'AdminActivitiesCategoryController@index')->name('activity-categories');
Route::name('activity-categories')->get('/activity/categories', 'AdminActivitesCategoryController@index');
//Route::get('/activity/{id}/categories', 'AdminActivitiesCategoryController@byType')->name('activities-categories-type');
Route::name('activity-categories-type')->get('/activity/{id}/categories', 'AdminActivitiesCategoryController@byType');
The commented lines result in the route not defined error. If name is called first then it works as it should.
The error also only occurs when the route() helper function is called in the blade template. Not when navigating to the route itself.
Next ErrorException: Route [admin.activity-categories-type] not defined. (View: C:inetpubwwwrootappraisal-meritresourcesviewsadminactivitiestypesall.blade.php) in C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingUrlGenerator.php:304
Stack trace:
#0 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateViewEnginesPhpEngine.php(44): IlluminateViewEnginesCompilerEngine->handleViewException(Object(InvalidArgumentException), 1)
#1 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateViewEnginesCompilerEngine.php(59): IlluminateViewEnginesPhpEngine->evaluatePath('C:\inetpub\wwwr...', Array)
#2 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateViewView.php(137): IlluminateViewEnginesCompilerEngine->get('C:\inetpub\wwwr...', Array)
#3 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateViewView.php(120): IlluminateViewView->getContents()
#4 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateViewView.php(85): IlluminateViewView->renderContents()
#5 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateHttpResponse.php(38): IlluminateViewView->render()
#6 C:inetpubwwwrootappraisal-meritvendorsymfonyhttp-foundationResponse.php(201): IlluminateHttpResponse->setContent(Object(IlluminateViewView))
#7 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingRouter.php(617): SymfonyComponentHttpFoundationResponse->__construct(Object(IlluminateViewView))
#8 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingRouter.php(574): IlluminateRoutingRouter->prepareResponse(Object(IlluminateHttpRequest), Object(IlluminateViewView))
#9 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(30): IlluminateRoutingRouter->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#10 C:inetpubwwwrootappraisal-meritappHttpMiddlewareCheckRole.php(26): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#11 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(148): AppHttpMiddlewareCheckRole->handle(Object(IlluminateHttpRequest), Object(Closure), 'administrator')
#12 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#13 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingMiddlewareSubstituteBindings.php(41): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#14 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(148): IlluminateRoutingMiddlewareSubstituteBindings->handle(Object(IlluminateHttpRequest), Object(Closure))
#15 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#16 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateAuthMiddlewareAuthenticate.php(43): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#17 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(148): IlluminateAuthMiddlewareAuthenticate->handle(Object(IlluminateHttpRequest), Object(Closure))
#18 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#19 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareVerifyCsrfToken.php(65): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#20 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(148): IlluminateFoundationHttpMiddlewareVerifyCsrfToken->handle(Object(IlluminateHttpRequest), Object(Closure))
#21 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#22 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateViewMiddlewareShareErrorsFromSession.php(49): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#23 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(148): IlluminateViewMiddlewareShareErrorsFromSession->handle(Object(IlluminateHttpRequest), Object(Closure))
#24 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#25 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateSessionMiddlewareStartSession.php(64): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#26 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(148): IlluminateSessionMiddlewareStartSession->handle(Object(IlluminateHttpRequest), Object(Closure))
#27 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#28 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateCookieMiddlewareAddQueuedCookiesToResponse.php(37): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#29 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(148): IlluminateCookieMiddlewareAddQueuedCookiesToResponse->handle(Object(IlluminateHttpRequest), Object(Closure))
#30 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#31 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateCookieMiddlewareEncryptCookies.php(59): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#32 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(148): IlluminateCookieMiddlewareEncryptCookies->handle(Object(IlluminateHttpRequest), Object(Closure))
#33 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#34 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(102): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#35 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingRouter.php(576): IlluminatePipelinePipeline->then(Object(Closure))
#36 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingRouter.php(535): IlluminateRoutingRouter->runRouteWithinStack(Object(IlluminateRoutingRoute), Object(IlluminateHttpRequest))
#37 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingRouter.php(513): IlluminateRoutingRouter->dispatchToRoute(Object(IlluminateHttpRequest))
#38 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateFoundationHttpKernel.php(176): IlluminateRoutingRouter->dispatch(Object(IlluminateHttpRequest))
#39 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(30): IlluminateFoundationHttpKernel->IlluminateFoundationHttp{closure}(Object(IlluminateHttpRequest))
#40 C:inetpubwwwrootappraisal-meritvendorbarryvdhlaravel-debugbarsrcMiddlewareDebugbar.php(51): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#41 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(148): BarryvdhDebugbarMiddlewareDebugbar->handle(Object(IlluminateHttpRequest), Object(Closure))
#42 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#43 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareTransformsRequest.php(30): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#44 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(148): IlluminateFoundationHttpMiddlewareTransformsRequest->handle(Object(IlluminateHttpRequest), Object(Closure))
#45 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#46 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareTransformsRequest.php(30): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#47 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(148): IlluminateFoundationHttpMiddlewareTransformsRequest->handle(Object(IlluminateHttpRequest), Object(Closure))
#48 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#49 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareValidatePostSize.php(27): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#50 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(148): IlluminateFoundationHttpMiddlewareValidatePostSize->handle(Object(IlluminateHttpRequest), Object(Closure))
#51 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#52 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareCheckForMaintenanceMode.php(46): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#53 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(148): IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode->handle(Object(IlluminateHttpRequest), Object(Closure))
#54 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateRoutingPipeline.php(53): IlluminatePipelinePipeline->IlluminatePipeline{closure}(Object(IlluminateHttpRequest))
#55 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminatePipelinePipeline.php(102): IlluminateRoutingPipeline->IlluminateRouting{closure}(Object(IlluminateHttpRequest))
#56 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateFoundationHttpKernel.php(151): IlluminatePipelinePipeline->then(Object(Closure))
#57 C:inetpubwwwrootappraisal-meritvendorlaravelframeworksrcIlluminateFoundationHttpKernel.php(116): IlluminateFoundationHttpKernel->sendRequestThroughRouter(Object(IlluminateHttpRequest))
#58 C:inetpubwwwrootappraisal-meritpublicindex.php(53): IlluminateFoundationHttpKernel->handle(Object(IlluminateHttpRequest))
#59 C:inetpubwwwrootappraisal-meritserver.php(21): require_once('C:\inetpub\wwwr...')
#60 {main}
A useful feature that shipped in Laravel 5.5 is fallback routing. You can learn about fallback routing in Better 404 responses using Laravel +5.5 by Mohamed Said (the author of the feature) to get the full picture of why it’s useful and how to use fallback routes.
When you are creating an API, you probably want a 404 route that responds with JSON (or whatever format you are serving via content negotiation) instead of the default 404 JSON response.
Here’s what you get if you request an undefined route with a JSON content type:
curl
-H"Content-Type:application/json"
-H"Accept: application/json"
-i http://apidemo.test/not/found
HTTP/1.1 404 Not Found
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: no-cache, private
Date: Thu, 16 Aug 2018 06:00:42 GMT
{
"message": ""
}
You can see that we get an empty message that isn’t necessarily super helpful, but the framework returns valid JSON without any work on our part. Let’s cover a couple of scenarios and walk through how you can ensure your API responds with a fallback 404 response with a useful message when an API route doesn’t match.
Setup
Using the Laravel CLI, we’ll create a new project to walk through adding a 404 response to your API:
laravel new apidemo
cd apidemo/
# Valet users...
valet link
We’ll configure a MySQL database for this project:
mysql -u root -e'create database apidemo'
Update the .env
to the credentials that match your database environment, for me it would be:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=apidemo
DB_USERNAME=root
DB_PASSWORD=
We’re going to use the users
table to demonstrate a few things. Update the database/seeds/DatabaseSeeder.php
to the following:
<?php
use IlluminateDatabaseSeeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
factory('AppUser', 10)->create();
}
}
Finally, you can run migrations and the seeder:
php artisan migrate:fresh --seed
API Routes
We’ll set up a few API routes, including the fallback route for our API, complete with a test. First, let’s create the following test:
php artisan make:test Api/FallbackRouteTest
Add a test case to look for a JSON response and verify a 404 error:
<?php
namespace TestsFeatureApi;
use TestsTestCase;
use IlluminateFoundationTestingWithFaker;
use IlluminateFoundationTestingRefreshDatabase;
class FallbackRouteTest extends TestCase
{
/** @test */
public function missing_api_routes_should_return_a_json_404()
{
$this->withoutExceptionHandling();
$response = $this->get('/api/missing/route');
$response->assertStatus(404);
$response->assertHeader('Content-Type', 'application/json');
$response->assertJson([
'message' => 'Not Found.'
]);
}
}
If you run the test suite at this point this test will fail because we haven’t defined a fallback route:
phpunit --filter=ApiFallbackRouteTest
...
There was 1 error:
1) TestsFeatureApiFallbackRouteTest::missing_api_routes_should_return_a_json_404
SymfonyComponentHttpKernelExceptionNotFoundHttpException: GET http://localhost/api/missing/route
Let’s define a fallback route at the end of the routes/api.php
file:
Route::fallback(function(){
return response()->json(['message' => 'Not Found.'], 404);
})->name('api.fallback.404');
We’ve created a fallback route that responds with JSON and returns a message which we assert in our test. We also assert the content type to be application/json
as well.
Note the name that we’ve defined for the route api.fallback.404
. We’ll need to use this name shortly to respond to a few exception types in our exception handler.
A Valid Users Endpoint
To illustrate further how the fallback route is used, we’ll define a valid route for our User
model:
Route::get('/users/{user}', 'UsersController@show')
->name('api.users.show');
Next, we’ll create a controller and user resource:
php artisan make:controller UsersController
php artisan make:resource User
We rely on implicit route model binding, and our controller uses the User resource to respond with JSON:
<?php
namespace AppHttpControllers;
use AppUser;
use AppHttpResourcesUser as UserResource;
use IlluminateHttpRequest;
class UsersController extends Controller
{
public function show(User $user)
{
return new UserResource($user);
}
}
Next, we’re going to create a test to verify the user endpoint and also verify that we get a 404 back when we request an invalid user:
php artisan make:test Api/ViewUserTes
This is where it gets interesting: let’s create a test to verify our route triggers a ModelNotFoundException
via route model binding:
<?php
namespace TestsFeatureApi;
use TestsTestCase;
use IlluminateFoundationTestingWithFaker;
use IlluminateDatabaseEloquentModelNotFoundException;
use IlluminateFoundationTestingRefreshDatabase;
class ViewUserTest extends TestCase
{
/** @test */
public function requesting_an_invalid_user_triggers_model_not_found_exception()
{
$this->withoutExceptionHandling();
try {
$this->json('GET', '/api/users/123');
} catch (ModelNotFoundException $exception) {
$this->assertEquals('No query results for model [AppUser].', $exception->getMessage());
return;
}
$this->fail('ModelNotFoundException should be triggered.');
}
}
This test is valid and should pass—it’s mostly making sure that somehow our controller is either using route model binding or is triggering a model exception somehow. We’ll come back to it in a second, but note that you may want a custom response message for the ModelNotFoundException
.
A somewhat redundant test, we will not disable exception handling and make sure we are getting back a 404 and the “no query results” error message back from the API:
/** @test */
public function requesting_an_invalid_user_returns_no_query_results_error()
{
$response = $this->json('GET', '/api/users/123');
$response->assertStatus(404);
$response->assertHeader('Content-Type', 'application/json');
$response->assertJson([
'message' => 'No query results for model [AppUser].'
]);
}
At this point both tests should be passing—we didn’t necessarily use TDD to drive out the route model binding.
Next, let’s write a failing test for our route model binding which ensures an invalid user id doesn’t match the route, and we get the fallback route:
/** @test */
public function invalid_user_uri_triggers_fallback_route()
{
$response = $this->json('GET', '/api/users/invalid-user-id');
$response->assertStatus(404);
$response->assertHeader('Content-Type', 'application/json');
$response->assertJson([
'message' => 'Not Found.'
]);
}
If you run this test you’ll get (partially) the following error:
Failed asserting that an array has the subset Array &0 (
'message' => 'Not Found.'
).
--- Expected
+++ Actual
@@ @@
Array
(
- [message] => Not Found.
+ [message] => No query results for model [AppUser].
We can get this test passing by constraining the {user}
route parameter:
Route::get('/users/{user}', 'UsersController@show')
->name('api.users.show')
->where('user', '[0-9]+');
If you run the test it will be passing, thus, successfully hitting the API fallback route:
phpunit --filter=invalid_user_uri_triggers_fallback_route
...
OK (1 test, 4 assertions)
It’s a good idea to add conditions to your route parameters so that the route will only match valid parameters and hit the fallback route otherwise. If using a fallback isn’t essential to you, the route model binding will still return a 404 error, but some databases might trigger an error while querying a table with an invalid value.
You would probably have something like this test as well, ensuring that your endpoint works for valid users:
use RefreshDatabase;
/** @test */
public function guests_can_view_a_valid_user()
{
$user = factory('AppUser')->create([
'name' => 'LeBron James',
'email' => 'lebron@lakers.com',
]);
$response = $this->json('GET', "/api/users/{$user->id}");
$response->assertOk();
$response->assertJson([
'data' => [
'name' => 'LeBron James',
'email' => 'lebron@lakers.com',
]
]);
}
Customizing ModelNotFoundException Responses
You might be fine with the error message that the API responds with when a ModelNotFoundException
is triggered through our route model binding. If you’re interested in triggering the fallback route, you could update the exception handler with a check that resembles the following:
# app/Exceptions/Handler.php
use IlluminateDatabaseEloquentModelNotFoundException;
use IlluminateSupportFacadesRoute;
public function render($request, Exception $exception)
{
if ($exception instanceof ModelNotFoundException && $request->isJson()) {
return Route::respondWithRoute('api.fallback.404');
}
return parent::render($request, $exception);
}
Again, this is an optional step that gives you an idea of how you can use exception handling coupled with a fallback route. You might be perfectly fine with the JSON response provided by default for a ModelNotFoundException
. The main point is demonstrating how to use fallback routes from the exception handler.
If you update the exception handler to include the check for a model not found, you’ll need to update the broken test to match the fallback response to get the test passing again.
Learn More
I’d encourage you to read through Mohamed Said’s Better 404 responses using Laravel +5.5 post and his pull request for the feature 5.5 Add ability to set a fallback route by themsaid . Our release announcement post Laravel 5.5.5 Released With a New Route Fallback to Help Customize Your 404 Views has more information on this feature as well.
I’m using laravel 5.8.24 in Ubuntu 18.04 And This is the first time I’m writing an API. I’ll add a test code here.
routes/api.php
Route::get('/api',function(){
return "Test api";
});
When I go to http://my_localhost_link/api
it gives a 404 error
Routes in routes/web.php
are working fine.
Can please anybody tell me what did I miss or what went wrong?
Thank you.
M.A.K. Ripon
2,8992 gold badges24 silver badges36 bronze badges
asked Jun 27, 2019 at 4:58
1
This can be resolved from apache, if you enable mod_rewrite option. So you need to run
sudo a2enmod rewrite
Changes in the file /etc/apache2/apache2.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Restart apache:
sudo service apache2 restart
answered Sep 4, 2019 at 11:24
3
for route/api.php
, you need to add /api/
prefix to your uri
.
for example:
Route::get('/api',function(){
return "Test api";
});
you need to call http://my_localhost_link/api/someroute
.
answered Sep 7, 2019 at 5:37
sooonsooon
1363 bronze badges
Did you run php artisan serve --port 8000
command ? Default middleware of serve is /api. Your url also /api. Run command and the open http://localhost:8000/api/api
answered Jun 27, 2019 at 6:46
laravel api routes difficulty have api prefix you can change/remove it on app/Providers/RouteServiceProvider.php
$this->routes(function () {
Route::prefix('api') <-HERE
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
also you can run
php artisan route:list
answered Apr 24, 2022 at 6:34
24 votes
89 answers
Get the solution ↓↓↓
When I try to access my laravel site I get this error in the console.
Laravel development server started: <http://127.0.0.1:8000>
[Mon Nov 16 10:39:15 2020] PHP Fatal error: Uncaught InvalidArgumentException: Route [home] not defined. in /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:389
Stack trace:
#0 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(822): IlluminateRoutingUrlGenerator->route('home', Array, true)
#1 /Users/threeaccents/code/src/gitlab.com/few/bodylove/storage/framework/views/e071ac62e490c49233841ae8b6b3906075bc0187.php(6): route('home')
#2 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(43): include('/Users/threeacc...')
#3 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(59): IlluminateViewEnginesPhpEngine->evaluatePath('/Users/threeacc...', Array)
#4 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/View/View.php(142): IlluminateVi in /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php on line 389
[Mon Nov 16 10:39:15 2020] PHP Fatal error: Uncaught InvalidArgumentException: Route [home] not defined. in /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:389
Stack trace:
#0 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php(822): IlluminateRoutingUrlGenerator->route('home', Array, true)
#1 /Users/threeaccents/code/src/gitlab.com/few/bodylove/storage/framework/views/e071ac62e490c49233841ae8b6b3906075bc0187.php(6): route('home')
#2 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(43): include('/Users/threeacc...')
#3 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(59): IlluminateViewEnginesPhpEngine->evaluatePath('/Users/threeacc...', Array)
#4 /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/View/View.php(142): IlluminateVi in /Users/threeaccents/code/src/gitlab.com/few/bodylove/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php on line 389
But route home is clearly defined. I even set it at the top of my route file to make sure nothing else was overriding it.
routes/web.php
<?php
// New Home Page for App
Route::get('/', 'Web[email protected]')->name('home');
...
withphp artisan route:list
+--------+
Update
I’ve cleared route cache, deleted vendor folder, re-installed PHP but nothing seems to solve the issue.
I’ve tried it on my work laptop (same setup) and everything works. It seems to be an issue with my current station but I’m not sure what would be causing it. I’m using PHP 7.2 on a mac os 10.14.4. Laravel 5.7
I also thought maybe it was an overall system issue but if I create a new laravel project everything works as expected so it does seem to be a project specific issue.
2021-11-14
Write your answer
745
votes
465
votes
676
votes
667
votes
555
votes
Answer
———+
| Domain | Method | URI | Name | Action | Middleware |
+———+
329
votes
486
votes
517
votes
314
votes
253
votes
Answer
———+
| | GET|HEAD | .well-known/apple-developer-merchantid-domain-association | | AppHttpControllersWebApplePayDomainVerificationController | web |
| | GET|HEAD | / | home | AppHttpControllersWeb[email protected] | web |
745
votes
Answer
Solution:
It was an issue with my apache config. It seems on my latest brew update apache got updated and the config file changed a bit. I had to change
#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
AllowOverride none
Require all denied
</Directory>
to
<Directory />
AllowOverride all
Require all granted
</Directory>
616
votes
Answer
Solution:
Route like that is doesn’t work except you edit the RouteServiceProvider
There is new version of laravel, please make sure you upgrade your PHP or XAMPP and try to update the composer global laravel and last create a new laravel project.
See Laravel documentation
And this post
https://youtu.be/MfE1tnMG6fE
375
votes
Answer
Solution:
You can remove name(‘home’), because error is Route [home] not defined
<?php
// New Home Page for App
Route::get('/', 'Web[email protected]');
Share solution ↓
Additional Information:
Date the issue was resolved:
2021-11-14
Link To Source
Link To Answer
People are also looking for solutions of the problem: undefined array key php
Didn’t find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.