laminas / laminas-view

Flexible view layer supporting and providing multiple view layers, helpers, and more
https://docs.laminas.dev/laminas-view/
BSD 3-Clause "New" or "Revised" License
74 stars 46 forks source link

Can't return a JsonModel in Controller. #225

Closed Huanga-IT-Solutions closed 1 year ago

Huanga-IT-Solutions commented 1 year ago

Bug Report

Q A
Version(s) 2.23.0 (my installed version)

Summary

Can't return a JsonModel in Controller. Laminas tries to render a View - Template. It seems that Laminas confuses the JsonModel for a ViewModel.

Current behavior

Code:


use Laminas\View\Model\JsonModel;
use Laminas\View\Model\ViewModel;

...

public function searchAction() {

        return new JsonModel([
            'msg' => 'test',
        ]);
    }

When I try to return a JsonModel, following fatal error message occurs:

Screenshot from 2023-10-23 12-25-08

How to reproduce

I'm using Docker / Docker - Compose. Here is my Docker / Docker - Compose - File:

FROM php:7.4-apache

ARG mode="dev" 

RUN apt-get update \
 && apt-get install -y git zlib1g-dev libzip-dev libicu-dev libpng-dev wget \
 && docker-php-ext-configure intl \
 && docker-php-ext-install intl \
 && docker-php-ext-install pdo pdo_mysql \
 && docker-php-ext-install zip \
 && docker-php-ext-install gd \
 && docker-php-ext-install pcntl \
 && a2enmod rewrite \
 && sed -i 's!/var/www/html!/var/www/public!g' /etc/apache2/sites-available/000-default.conf \
 && mv /var/www/html /var/www/public \
 && curl -sS https://getcomposer.org/installer \
  | php -- --install-dir=/usr/local/bin --filename=composer

RUN apt install nodejs -y
RUN apt-get install npm -y

RUN yes | pecl install xdebug-2.9.8

COPY php.ini /usr/local/etc/php/

WORKDIR /var/www

COPY . .

RUN if [ "$mode" = "prod" ] ; then composer install --no-dev; else composer install; fi
RUN npm install 

RUN apt-get update
RUN yes | apt-get install gnupg
RUN wget -O phive.phar "https://phar.io/releases/phive.phar"
RUN wget -O phive.phar.asc "https://phar.io/releases/phive.phar.asc"
RUN gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x6AF725270AB81E04D79442549D8A98B29B2D5D79
RUN gpg --verify phive.phar.asc phive.phar
RUN rm phive.phar.asc
RUN chmod +x phive.phar
RUN mv phive.phar /usr/local/bin/phive
RUN yes | phive install  phpdocumentor

COPY opcache.ini /usr/local/etc/php/conf.d/opcache.ini
RUN docker-php-ext-configure opcache --enable-opcache \
    && docker-php-ext-install opcache

RUN chmod -R 777 data/cache
version: '3.5'

services:

  laminas-app:
    container_name: laminas-app
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "87:80"
      - "9503:9502"
    volumes:
      - ./:/var/www
    depends_on:
      - laminas-mysql
    networks:
      - app_network

  laminas-mysql:
    container_name: laminas-mysql
    image: mariadb:10.4.13
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: r00tPW!/
      MYSQL_DATABASE: laminasdb
      MYSQL_USER: laminasuser
      MYSQL_PASSWORD: laminaspassword
    ports:
      - "3312:3306"
    networks:
      - app_network

  laminas-phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    restart: always
    container_name: laminas-pma
    links:
        - laminas-mysql
    environment:
        PMA_HOST: laminas-mysql
        PMA_USER: root
        PMA_PASSWORD: r00tPW!/
    ports:
    - 8081:80
    networks:
    - app_network

networks:
  app_network:

Expected behavior

No Error Message, Return JSON - Response to Client.

Thanks for your Help :)

gsteel commented 1 year ago

You need to ensure that the "Json Rendering Strategy" is enabled in your MVC app.

Your configuration will need to include the following:

'view_manager' => [
   'strategies' => [
      'ViewJsonStrategy',
   ],
];

To be fair, I don't think that this is covered in the docs in either laminas-view or laminas-mvc.

Whilst this article is pretty old, it should still illustrate the same required configuration with a bit more context.

Huanga-IT-Solutions commented 1 year ago

Hi George,

Thanks, that worked :)

BR, Valentin

froschdesign commented 1 year ago

@gsteel

To be fair, I don't think that this is covered in the docs in either laminas-view or laminas-mvc.

Please see: "Quick Start – Creating and Registering Alternate Rendering and Response Strategies":

You could also use the module configuration to add the strategies:

namespace Application;

use Laminas\ModuleManager\Feature\ConfigProviderInterface;

class Module implements ConfigProviderInterface
{
    /**
     * Returns configuration to merge with application configuration
     *
     * @return array
     */
    public function getConfig()
    {
        return [
            /* ... */
            'view_manager' => [
                /* ... */
                'strategies' => [
                    'ViewJsonStrategy',
                ],
            ],
        ];
    }
}
gsteel commented 1 year ago

Thanks @froschdesign 👍

froschdesign commented 1 year ago

The corresponding hint can also be found in the documentation of laminas-view: https://docs.laminas.dev/laminas-mvc/services/#default-configuration-options Not ideal but present.