美文网首页
laravel写api接口

laravel写api接口

作者: 紫微圣君 | 来源:发表于2018-03-12 16:47 被阅读0次

简要描述:

  • 在laravel根目录下面的routes目录下,有几个文件。其中web.php为网页的路由配置文件,而api.php就是api的路由配置文件。

举例:

<?php

/*
|--------------------------------------------------------------------------
| 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!
|
*/

// api
Route::group(array('domain' => 'abc.com'), function()
{
    Route::any('/', function() {
        $ctrl = \App::make("\\App\\Http\\Controllers\\" . ucfirst('api') . "\\" . ucfirst('index') . "Controller");
        return \App::call([$ctrl, ucfirst('index')]);
    });
});

注意:

  • 与web路由不同,访问API路由,是需要在域名后面加上api的。比如:访问api模块下,index控制器中的index方法,地址如下:
https://abc.com/api/

相关文章

网友评论

      本文标题:laravel写api接口

      本文链接:https://www.haomeiwen.com/subject/rwznfftx.html