douxi4414 2017-04-21 12:55
浏览 189

Laravel routes / web.php和routes / api.php

The routes/web.php have common application routes, Like showing the views, Getting the data from the form to a controller method.

The routes/api.php will have routes for getting records of the tables/entities in JSON. Updating, Deleting etc via api routes ?

Question 1 : How will i use my routes/api.php routes to get, delete, Update data/Records ?

Question 2 : Android app will be able to use my api ? If yes, How ?

Question 3 : The API should have a controller ApiController ?

Let the kid know the stuff !

Thanks

  • 写回答

1条回答 默认 最新

  • dsag14654 2017-04-21 13:08
    关注

    1:

    Put this into your RouteServiceProvider file:

        $router->group(['namespace' => $this->webNamespace], function ($router) {
            require app_path('routes/web.php');
        });
    
    
        $router->group(['namespace' => $this->apiNamespace], function ($router) {
            require app_path('routes/api.php');
        });
    

    1.1 in routes/web.php file (alternative):

    require_once "../../routes/api.php";
    

    2:

    yes. use your routes as you would use them in any other frontend application. For example: localhost/api/myAndroidRoute

    3:

    doesn't really matter. you can use one controller for all the routes, or one controller for each route. Whichever option you like. If you have a lot of code, separate it into different controllers for better readibility. For example:

    Route::get('api/sampledata', 'ApiController@getSampleData'); // controller@function
    Route::get('api/otherdata', 'SomeOtherController@getOtherData');
    

    or

    Route::get('api/sampledata', 'AllmightyController@getSampleData');
    Route::get('web/otherdata', 'AllmightyController@getOtherData');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大