wdgwgz

990 经验值

Laravel版本:5.5
dingo 版本:1.0.0-beta8

Dingo的Transformer

在Controller里用 $this->collection($xx,new xxTransformers) 的方式 return出去的JSON格式是这样的

{
data:[

  {},
  {}

]
}

如果我想在根节点添加 status_code、message参数呢?就像这样

{
status_code:200,
message:"success"
data:[

  {},
  {}

]
}

怎么做?

数据是正确的,
User::find(1)->UserInfo(); 不行
试了下:User::find(1)->UserInfo 可以了,这是为什么呢?

Eloquent ORM 一对一关联失败,使用的Laravel 5.5

有张users表,字段为 id、email、password 等等
有张user_infos表,字段为 id、user_id、avatar等等

在User的Model里定义了个 UserInfo方法
public function UserInfo(){

    return $this->hasOne('App\Models\UserInfoModel','user_id','id');
}

Controller里调用
User::find(1)->UserInfo();
得到的Model是空的,这个model转换为JSON是 :{}

transform哪些场景下用 哪些场景下不用呢?
不需要修改表字段名的地方就不使用吗?
可是我在github看到好多项目transform都是不修改字段名或者值的,比如这个transform 有啥意义呢?

Alt text

在BaseController的class 引入了一句 use Helpers; (Dingo\Api\Routing\Helpers;)
请问这个Helpers的使用文档在哪?

JWT登录JWTAuth::attempt只能验证邮箱+密码,如果登录接口需要同时还兼容:用户名+密码,邮箱+密码,手机号+密码,该怎么做呢?

自己从数据库判断如手机号+密码,验证通过之后,JWTAuth::fromUser($user); 返回token 可以吗?

-------------分隔线,解决了------------

JWTAuth::attempt传入数组,key 对应数据库字段名即可 就可以验证,比如我的代码

    //把账号设置到 name、email、phone字段里
    $request->merge([
        'name' => $request->get('account'),
        'email' => $request->get('account'),
        'phone' => $request->get('account'),
    ]);

    try {
        if ( $token = JWTAuth::attempt($request->only('name', 'password')) ||
             $token = JWTAuth::attempt($request->only('email', 'password')) ||
             $token = JWTAuth::attempt($request->only('phone', 'password'))  ){
                $user = Auth::user();
                $token = JWTAuth::fromUser($user);
        }else{
            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

昨天Dingo、JWT在服务器中终于安装好了,Dingo定义的路由也可以访问了,然后今天git clone到homestead环境下,修改.env数据库信息,执行composer update 、 php artisan migrate。访问默认首页(Laravel自带)正常,访问Dingo定义的路由却报:NotFoundHttpException

具体报错如下:

(1/1) NotFoundHttpException
in RouteCollection.php (line 179)
at RouteCollection->match(object(Request))
in Router.php (line 548)
at Router->findRoute(object(Request))
in Router.php (line 527)
at Router->dispatchToRoute(object(Request))
in Router.php (line 513)
at Router->dispatch(object(Request))
in Kernel.php (line 174)
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
in Pipeline.php (line 30)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ValidatePostSize.php (line 27)
at ValidatePostSize->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in CheckForMaintenanceMode.php (line 46)
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 102)
at Pipeline->then(object(Closure))
in Kernel.php (line 149)
at Kernel->sendRequestThroughRouter(object(Request))
in Kernel.php (line 116)
at Kernel->handle(object(Request))
in index.php (line 53)

但是现在遇到一个新问题,api.php代码如下:

$api = app('Dingo\Api\Routing\Router');

$api->version('v1', function ($api) {
    $api->group(['namespace'=>'App\Api\Controllers'],function ($api){

        $api->get('lessons','LessonsController@index');

    });
});

执行:php artisan api:routes
结果:Your application doesn't have any routes.

是因为我的Laravel版本是5.5的原因吗?

解决了,根据Github issues的回答,更新到Dingo1.0 beta8版本就可以了