zoukankan      html  css  js  c++  java
  • laravel疑难问题---5、laravel的api开发

    laravel疑难问题---5、laravel的api开发

    一、总结

    一句话总结:

    写api的路由在routes文件夹下的api.php里面,访问的话记得在域名和路由中间接上api,例如 api.com/api/news
    <?php
    
    use IlluminateHttpRequest;
    use IlluminateSupportFacadesRoute;
    
    /*
    |--------------------------------------------------------------------------
    | API Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register API routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | is assigned the "api" middleware group. Enjoy building your API!
    |
    */
    
    Route::middleware('auth:api')->get('/user', function (Request $request) {
        return $request->user();
    });
    Route::get('/news2', 'ApiHomeNewsController@getNews');
    Route::get('/news', function () {
        $data_back=[];
        $data_back['code']=1;
        $data_back['message']='success';
        $news=[];
        $news_per=[];
        $news_per['title']='中国数字经济全面提速';
        $news_per['content']='随着境外疫情加速扩散蔓延,国际经贸活动受到严重影响,我国经济发展面临新的挑战,同时也给我国加快科技发展、推动产业优化升级带来新的机遇。从今天(14日)开始,《新闻联播》推出《迎难而上 创新发展》系列报道,聚焦各地各部门准确识变、科学应变、主动求变,从危机中创造机遇,在攻坚克难中创新发展,坚定不移贯彻新发展理念,努力实现经济高质量发展的新突破。今天先来关注我国数字经济全面提速。';
        $news[]=$news_per;
        $news_per=[];
        $news_per['title']='英国首相出院,他特别感谢了谁';
        $news_per['content']='我想特别提到两位护士,希望他们别介意。他们在我病情处于恶化和改善的关口时,站在我床边整整48个小时。';
        $news[]=$news_per;
        $data_back['news']=$news;
        return $data_back;
    });

    二、laravel的api开发

    博客对应课程的视频位置:5、laravel的api开发
    https://www.fanrenyi.com/video/9/201

    写api的路由在routes文件夹下的api.php里面,访问的话记得在域名和路由中间接上api,例如 api.com/api/news

    1、路由

    写api的路由在routes文件夹下的api.php里面

     1 <?php
     2 
     3 use IlluminateHttpRequest;
     4 use IlluminateSupportFacadesRoute;
     5 
     6 /*
     7 |--------------------------------------------------------------------------
     8 | API Routes
     9 |--------------------------------------------------------------------------
    10 |
    11 | Here is where you can register API routes for your application. These
    12 | routes are loaded by the RouteServiceProvider within a group which
    13 | is assigned the "api" middleware group. Enjoy building your API!
    14 |
    15 */
    16 
    17 Route::middleware('auth:api')->get('/user', function (Request $request) {
    18     return $request->user();
    19 });
    20 Route::get('/news2', 'ApiHomeNewsController@getNews');
    21 Route::get('/news', function () {
    22     $data_back=[];
    23     $data_back['code']=1;
    24     $data_back['message']='success';
    25     $news=[];
    26     $news_per=[];
    27     $news_per['title']='中国数字经济全面提速';
    28     $news_per['content']='随着境外疫情加速扩散蔓延,国际经贸活动受到严重影响,我国经济发展面临新的挑战,同时也给我国加快科技发展、推动产业优化升级带来新的机遇。从今天(14日)开始,《新闻联播》推出《迎难而上 创新发展》系列报道,聚焦各地各部门准确识变、科学应变、主动求变,从危机中创造机遇,在攻坚克难中创新发展,坚定不移贯彻新发展理念,努力实现经济高质量发展的新突破。今天先来关注我国数字经济全面提速。';
    29     $news[]=$news_per;
    30     $news_per=[];
    31     $news_per['title']='英国首相出院,他特别感谢了谁';
    32     $news_per['content']='我想特别提到两位护士,希望他们别介意。他们在我病情处于恶化和改善的关口时,站在我床边整整48个小时。';
    33     $news[]=$news_per;
    34     $data_back['news']=$news;
    35     return $data_back;
    36 });
     

    2、访问

    访问的话记得带上api

    http://api.com/api/news

    http://api.com/api/news2

     
  • 相关阅读:
    【NOIP2017】跳房子
    MySQL的多表查询(笛卡尔积原理)
    MySQL的delete误操作的快速恢复方法
    MYSQL高可用集群架构-MHA架构
    mysql数据库的创建问题
    Python中xlrd和xlwt模块使用方法----》》数据库数据导出(之一)
    mysql数据库的批量数据导入与导出,性能提升。
    使用Python对ElasticSearch获取数据及操作
    SQL语句
    ElasticSearch-排序
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/12702386.html
Copyright © 2011-2022 走看看