Distinguish between a call from web route vs API route?

六眼飞鱼酱① 提交于 2020-05-28 03:16:11

问题


In my web.php file, I have a route that looks like this:

Route::get('/', 'HomeController@getFeed');

And in my api.php file, I have a route that looks like this:

Route::get('feeds', 'HomeController@getFeed');

Notice that they both call the same method, getFeed().

Is there a way to distinguish whether the call came from the web route vs the API route in the controller's method? I need to be able to return two different responses, one for the web route and one for the API route.

Here is the HomeController.php class:

class HomeController extends Controller
{
    public function getFeed() {
        $user = Auth::user();

        // How to check if call is from web route or API route?
        // Need to return two different responses for each scenario.
    }
}

Thanks.


回答1:


All routes from api.php are automatically prefixed with 'api/' So you can use he below code to check

    if (Request::is('api*')) {
        echo "request from api route";
        exit();
    }else{
        echo "request from web";
        exit();
    }



回答2:


replace this line
Route::get('feeds', 'HomeController@getFeed'); with Route::get('api/feeds', 'HomeController@getFeed');

means add api prefix in all of your api routes.

it will help you to identify which route come from api and which not.

to check you can use below code

if (Request::is('api*')) {
    echo "request from api route";
    exit();
}else{
    echo "request from web";
    exit();
}



回答3:


I use \Request::is('api/*')

if(\Request::is('api/*'))
   return 'API';
else
   return 'Non-API';

Alternatively you can check using the route name;

if(\Request::route()->getName() == 'APIFeed')
   return 'API';
else
   return 'Non-API';

Make sure to set the route name;

In web.php

Route::get('feeds', 'HomeController@getFeed')->name('WebFeed');

In api.php

Route::get('feeds', 'HomeController@getFeed')->name('APIFeed');



回答4:


You can use

$currentRoute = Illuminate\Routing\Router::getCurrentRoute(); // Returns a Route
$currentRoute->uri(); // returns the uri of the cureent route 

https://laravel.com/api/5.4/Illuminate/Routing/Router.html#method_getCurrentRoute

https://laravel.com/api/5.4/Illuminate/Routing/Route.html

Hope this helps. :)




回答5:


In your shoes, I would have created 3 controllers to handle the requests and like Niraj proposed, separate routes by prefix the api route with /api/

class HomeController extends Controller
{
  public function getFeed(entrypoint) {
    $user = Auth::user();
    // do the common magic here ...
  }
}


class WebHomeController extends HomeController 
{
    public function getFeed() {
        feed = this.getFeed();
        // do the crazy web magic here ...
    }
}

class APIHomeController extends HomeController 
{
    public function getFeed() {
        feed = this.getFeed();
        // do the crazy api magic here ...
    }
}

Route::get('feeds', 'WebHomeController@getFeed'); 
Route::get('api/feeds', 'APIHomeController@getFeed');


来源:https://stackoverflow.com/questions/43383698/distinguish-between-a-call-from-web-route-vs-api-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!