You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
I've been trying to use apigility with doctrine for a long time now, but due the updates it was really hard to find a compatible version and now I'm having a weird behavior.
I will put all the steps I made here so you can reproduce and see for yourself.
The problem here is: I have doctrine orm module installed, configured, and I'm getting the information, it's returning everything if I var_dump or print_r, however when I try to pass that information (array) via return $users on the resource method, all that appears is a blank page.
1 - Install the apigility version 1.3.1 (The only version that composer didn't screamed with doctrine orm module compatibility errors):
<?php
namespace API\V1\Rest\Users;
use Doctrine\ORM\EntityManagerInterface;
use User\Entity\User;
use ZF\ApiProblem\ApiProblem;
use ZF\Rest\AbstractResourceListener;
class UsersResource extends AbstractResourceListener
{
private $em;
public function __construct(EntityManagerInterface $em) {
$this->em = $em;
}
/**
* Create a resource
*
* @param mixed $data
* @return ApiProblem|mixed
*/
public function create($data) {
return new ApiProblem(405, 'The POST method has not been defined');
}
/**
* Delete a resource
*
* @param mixed $id
* @return ApiProblem|mixed
*/
public function delete($id) {
return new ApiProblem(405, 'The DELETE method has not been defined for individual resources');
}
/**
* Delete a collection, or members of a collection
*
* @param mixed $data
* @return ApiProblem|mixed
*/
public function deleteList($data) {
return new ApiProblem(405, 'The DELETE method has not been defined for collections');
}
/**
* Fetch a resource
*
* @param mixed $id
* @return ApiProblem|mixed
*/
public function fetch($id) {
$qb = $this->em->createQueryBuilder();
$qb->select('u');
$qb->from(User::class, 'u');
$qb->where('u.id = :id');
$qb->setParameters(array(
'id' => $id
));
$user = $qb->getQuery()->getArrayResult();
return $user;
}
/**
* Fetch all or a subset of resources
*
* @param array $params
* @return ApiProblem|mixed
*/
public function fetchAll($params = array()) {
$qb = $this->em->createQueryBuilder();
$qb->select('u');
$qb->from(User::class, 'u');
$users = $qb->getQuery()->getArrayResult();
return new UsersCollection($users);
}
/**
* Patch (partial in-place update) a resource
*
* @param mixed $id
* @param mixed $data
* @return ApiProblem|mixed
*/
public function patch($id, $data) {
return new ApiProblem(405, 'The PATCH method has not been defined for individual resources');
}
/**
* Replace a collection or members of a collection
*
* @param mixed $data
* @return ApiProblem|mixed
*/
public function replaceList($data) {
return new ApiProblem(405, 'The PUT method has not been defined for collections');
}
/**
* Update a resource
*
* @param mixed $id
* @param mixed $data
* @return ApiProblem|mixed
*/
public function update($id, $data) {
return new ApiProblem(405, 'The PUT method has not been defined for individual resources');
}
}
<?php
namespace API\V1\Rest\Users;
use Doctrine\ORM\EntityManager;
class UsersResourceFactory
{
public function __invoke($services)
{
$em = $services->get(EntityManager::class);
return new UsersResource($em);
}
}
<?php
namespace API\V1\Rest\Users;
use Zend\Paginator\Adapter\ArrayAdapter;
use Zend\Paginator\Paginator;
class UsersCollection extends Paginator
{
public function __construct($userCollection) {
parent::__construct(new ArrayAdapter($userCollection));
}
}
Now, if i try to access: 127.0.0.1:8080/users all that I got is a blank page. The weird thing is that doctrine is returning everything from the database, just enter on UsersResource::fetchAll() and add var_dump($users) you will see that the array is there with all the values.
What is happening, anyone knows?
The text was updated successfully, but these errors were encountered:
I've been trying to use apigility with doctrine for a long time now, but due the updates it was really hard to find a compatible version and now I'm having a weird behavior.
I will put all the steps I made here so you can reproduce and see for yourself.
The problem here is: I have doctrine orm module installed, configured, and I'm getting the information, it's returning everything if I
var_dump
orprint_r
, however when I try to pass that information (array) viareturn $users
on the resource method, all that appears is a blank page.1 - Install the apigility version 1.3.1 (The only version that composer didn't screamed with doctrine orm module compatibility errors):
composer create-project -sdev zfcampus/zf-apigility-skeleton:^1.3.1
2 - Update using composer (the --lock is to make sure it won't update to the latest versions and mess everything up)
composer update --lock
3 - Put the doctrine requirement on your
composer.json
:4 - Update using composer, again in order to add the doctrine to the vendor folder - and to the lock file as well:
composer update --lock
5 - Put the necessary modules information in order to load the modules -
config/modules.config.php
:6 - Creating the necessary configuration files:
config/autoload/doctrine-orm.global.php
:config/autoload/doctrine-orm.local.php
:7 - Enter in development mode:
php public/index.php development enable
8 - Creating the domain model module (
User
- under module folder):module/User/config/module.config.php
:module/User/src/User/Entity/User.php
:9 - Create the table on the database based on the entity:
php public/index.php orm:schema-tool:create
10 - Start apigility server and create an API called
API
and a REST service calledUsers
:php -S 0.0.0.0:8080 -t public public/index.php
11 - Change the files apigility created:
module/API/src/API/V1/Rest/Users/UsersResource.php
:module/API/src/API/V1/Rest/Users/UsersResourceFactory.php
:module/API/src/API/V1/Rest/Users/UsersCollection.php
:Now, if i try to access:
127.0.0.1:8080/users
all that I got is a blank page. The weird thing is that doctrine is returning everything from the database, just enter onUsersResource::fetchAll()
and addvar_dump($users)
you will see that the array is there with all the values.What is happening, anyone knows?
The text was updated successfully, but these errors were encountered: