1. Go to this page and download the library: Download godruoyi/dingo-api-helper library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
godruoyi / dingo-api-helper example snippets
$app['Dingo\Api\Transformer\Factory']->setAdapter(function ($app) {
$fractal = new League\Fractal\Manager;
$fractal->setSerializer(new League\Fractal\Serializer\ArraySerializer);
return new Dingo\Api\Transformer\Adapter\Fractal($fractal);
});
class ExampleController extends BaseController
{
public function __invoke()
{
$users = User::all();
return $this->response->collection($users, new UserTransformer);//每次都要手动指定
}
}
return $this->response->item($user);
return $this->response->item($user, new \Other\UserDetailTransformer);
Godruoyi\DingoApiHelper\ServiceProvider::class,
use Dingo\Api\Routing\Helpers;
class ExampleController extends Controller
{
use Helpers;
public function __invoke()
{
$users = User::all();
return $this->response->collection($users);
// return $this->response->collection($users, new OtherTransformer);
}
}
public function __invoke(Request $request)
{
$user = User::find($request->id);
return $this->response->item($user, new \Other\UserDetailTransformer);
}
namespace App\Transformers;
class UserTransformer extends TransformerAbstract
{
/**
* Include resources without needing it to be requested.
*
* @var array
*/
protected $defaultIncludes = ['notExistsRelacation'];
public function transform(User $user, $a)
{
}
}
class ExampleController extends Controller
{
use Helpers;
public function __invoke()
{
$users = User::all();
return $this->response->disableEagerLoading()->collection($users);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.