Search code examples
phpyii2yii2-advanced-appyii-url-manager

How to override or edit code of vendor directory in yii2?


I need to do some changes in vendor/yiisoft/yii2/web/urlmanager.php for my url_alias to work! I need to know if I can change this file directly or is there any method to override this file?


Solution

  • Best way is to create new URL Manager Class that extends existing UrlManager class i.e. yii/web/urlManager.php

    i.e

    class customUrlManager extends yii/web/urlManager {
    .. code here
    }
    

    Then specify the class in your config element i.e frontend/config/main.php

     'urlManager' => [
                'class' => '<name_space>/customUrlManager'
                'enablePrettyUrl' => True,
                'showScriptName' => False,
                'rules' => [
                    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                    '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
                ],
            ],