How to use API Routes in Laravel

0 votes

In Laravel API routes were moved into the api.php file. But how can I call a route in api.php file? I tried to create a route like this:

Route::get('/test',function(){
     return "ok"; 
});

I tried the following URLs but both returned the NotFoundHttpException exception:

  • http://localhost:8080/test/public/test
  • http://localhost:8080/test/public/api/test

How can I call this API route?

Aug 10, 2020 in Laravel by kartik
• 37,510 points
7,321 views

1 answer to this question.

0 votes

Hello @kartik,

You call it by

http://localhost:8080/api/test
                   

If you look in app/Providers/RouteServiceProvider.php you'd see that by default it sets the api prefix for API routes, which you can change of course if you want to.

protected function mapApiRoutes()
{
    Route::group([
        'middleware' => 'api',
        'namespace' => $this->namespace,
        'prefix' => 'api',
    ], function ($router) {
        require base_path('routes/api.php');
    });
}

Hope it helps!!

Thank You!!

answered Aug 10, 2020 by Niroj
• 82,880 points

Related Questions In Laravel

0 votes
1 answer

How to make a constant and use globally in laravel?

Hii, You can create a constants.php page in config folder ...READ MORE

answered Mar 24, 2020 in Laravel by Niroj
• 82,880 points
3,622 views
0 votes
1 answer

How to use multiple databases in Laravel?

Hello @kartik, Laravel has inbuilt support for multiple ...READ MORE

answered Jul 30, 2020 in Laravel by Niroj
• 82,880 points
2,106 views
0 votes
1 answer

How to only use created_at in Laravel?

Hello @kartik, Eloquent does not provide such functionality ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,880 points
2,691 views
0 votes
1 answer

How to use GROUP_CONCAT in laravel?

Hello @kartik, You can use relations as query ...READ MORE

answered Sep 25, 2020 in Laravel by Niroj
• 82,880 points
6,865 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,913 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,691 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,561 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
41,897 views
0 votes
1 answer

How to use Stored Procedures in Laravel?

Hey, To create a Stored Procedure you can ...READ MORE

answered Mar 24, 2020 in Laravel by Niroj
• 82,880 points
4,567 views
0 votes
1 answer

How to use mail() in laravel?

Hello, Laravel provides a powerful and clean API ...READ MORE

answered Mar 24, 2020 in Laravel by Niroj
• 82,880 points
640 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP