Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

I'm getting a blank page #174

Open
viyirenov opened this issue Aug 15, 2016 · 5 comments
Open

I'm getting a blank page #174

viyirenov opened this issue Aug 15, 2016 · 5 comments

Comments

@viyirenov
Copy link

viyirenov commented Aug 15, 2016

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):

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:

"require": {
    // ....
    "doctrine/doctrine-orm-module": "0.*"
},

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:

'DoctrineModule',
'DoctrineORMModule',

6 - Creating the necessary configuration files:

config/autoload/doctrine-orm.global.php:

<?php
return array(
    'doctrine' => array(
        'connection' => array(
            'orm_default' => array(
                'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
                'params' => array(
                    'host'     => 'localhost',
                    'port'     => '3306',
                ),
            ),
        ),
    ),
);

config/autoload/doctrine-orm.local.php:

<?php
return array(
    'doctrine' => array(
        'connection' => array(
            'orm_default' => array(
                'params' => array(
                    'user'     => 'root',
                    'password' => '',
                    'dbname'   => 'apigility',
                ),
            ),
        ),
    ),
);

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:

<?php
return array(
    'doctrine' => array(
        'driver' => array(
            'my_annotation_driver' => array(
                'class' => 'Doctrine\\ORM\\Mapping\\Driver\\AnnotationDriver',
                'cache' => 'array',
                'paths' => array(
                    0 => __DIR__ . '/../src/User/Entity',
                ),
            ),
            'orm_default' => array(
                'drivers' => array(
                    'User\\Entity' => 'my_annotation_driver',
                ),
            ),
        ),
    ),
);

module/User/src/User/Entity/User.php:

<?php

namespace User\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class User {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     *
     * @var string @ORM\Column(name="name", type="string", length=255, nullable=true)
     */
    private $name;

    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }
}

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 called Users:

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:

<?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');
    }
}

module/API/src/API/V1/Rest/Users/UsersResourceFactory.php:

<?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);
    }
}

module/API/src/API/V1/Rest/Users/UsersCollection.php:

<?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?

@viyirenov
Copy link
Author

@Wilt
Copy link

Wilt commented Aug 16, 2016

Blank page?
What is the status code?
Did you enable error reporting?

@viyirenov
Copy link
Author

@Wilt - The status code is 200 and the errors is enabled.

@diogoca
Copy link

diogoca commented Feb 11, 2017

@viyirenov did you figure out how to fix?

@weierophinney
Copy link
Member

This repository has been closed and moved to laminas-api-tools/api-tools; a new issue has been opened at laminas-api-tools/api-tools#19.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants