0
votes

I have a yii2 basic application with 2 parts (web and service for mobile).

I have created a module to handle the restful requests fired from mobile . I want to configure this module to be rest. So I created a config file for this module in side the module directory. as mentioned in the yii2 documentation for modules

/config/config.php:

return [

'components' => [
  'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'enableStrictParsing' => false,
        'rules' => array(

            [
                'class' => 'yii\rest\UrlRule',
                'controller' => 'mobile/mobile-clients',
                'extraPatterns' => ['GET search' => 'search']
            ],

        ),
    ],
    'request' => [
        'class' => '\yii\web\Request',
        'enableCookieValidation' => false,
        'parsers' => [
            'application/json' => 'yii\web\JsonParser',
        ],
    ],
]

];

the module class is as follows:

<?php

namespace app\modules\Mobile;

use Yii;
use yii\base\Module;

class MobileService extends Module {

     public $controllerNamespace = 'app\modules\Mobile\controllers';

     public function init() {
         parent::init();     
         Yii::configure($this, require(__DIR__ .DIRECTORY_SEPARATOR     
                   .'config'.DIRECTORY_SEPARATOR .'config.php'));

     }
}         

The problem is that the request component is not working as expected while it works fine when configured in the application configuration (config/main.php)

same for the urlManager.

Any Ideas?

1
Why would you want to duplicate those components? Just copy the instances so there is only one of each. Never a good idea to duplicate components used all over the place - Blizz
@robsch I want to customize the components of the mobile module to be restful that is all what I want, Is this the right way - Boudi
.../modules/Mobile/config/config.php This file does not exist? - Ngô Văn Thao
@Ngô Văn Thao the file exists - Boudi

1 Answers

0
votes

The Solution to my problem is to create api application that is a new application inside the yii2 basic app. It shares the models and the vendors directory but has its own configuration and entry script (index.php). This is the solution link for more information .

EDIT:

Do not forget to add the user component in the api.config file

  'user' => [
  'identityClass' => 'app\models\User',
  'enableAutoLogin' => false,
  ], 

I think using yii2 advanced application structure is better for a case like mine. But this solution works perfect :) .

Best.