How to configuration for module?

I want to use DIRECTORY STRUCTURE for module. see below


config/    -> general config

models/

modules

    api/

        components/

        config/main.php     -> Only config for api

        controllers/

        Module.php

    backend/

        components/

        config/main.php     -> Only config for backend

        controllers/

        views/

        Module.php

    frontend/

        components/

        config/main.php    -> Only config for frontend

        controllers/

        views/

        Module.php

web/

    index.php

    themes/

        backend/     -> theme for backend

        frontend/      -> theme for frontend

I use this guide http://www.yiiframework.com/doc-2.0/guide-structure-modules.html , http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html

And created main.php config for api




<?php

return [


    'components' => [

        // list of component configurations

        'request' => [

            'class' => 'yii\web\Request',

            'parsers' => [

                'application/json' => 'yii\web\JsonParser',

            ]

        ],

        'urlManager' => [

            'class' => 'yii\web\UrlManager',

            'enablePrettyUrl' => true,

            'enableStrictParsing' => true,

            'showScriptName' => false,

            'rules' => [

                ['class' => 'yii\rest\UrlRule', 'controller' => 'user'],

            ],

        ]

    ],

    'params' => [

        // list of parameters

    ],

];



And include it in init() function (Module.php file)


\Yii::configure($this, require(__DIR__ . '/main.php'));

And access to: base_url/api/users, but response is "Page not found."

Please help me

Check app.log’s

Is UsersController with actionIndex() in api module exist?

Hi @umneeq,

Controller and action is exist.

My problem is configuration for module.

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:


class Module extends \yii\base\Module

{


    /**

     * @inheritdoc

     */

    public function init()

    {

        parent::init();


        // custom initialization code goes here


        // initialize the module with the configuration loaded from config.php

        Yii::configure($this, require(__DIR__ . '/config/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?

use DI and try this:


public function init() {

        parent::init();

        

        

        ///Yii::configure($this, require(__DIR__ . '/config/main.php'));

        // custom initialization code goes here

    

        \Yii::$app->setComponents([

            'request' => [

                'class'=>\yii\web\Request::class,

                'parsers' => [

                    'application/json' => 'yii\web\JsonParser',

                ]

            ]

        ]);                            

    }