yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

How do i catch beforeAction event from any class and change layout? #3589

Closed mithun12000 closed 10 years ago

mithun12000 commented 10 years ago

I am trying to achieve this to create a theme. but i don't know how i can achieve. I have gone through you doc too.

cebe commented 10 years ago

why do you need beforeAction to change the layout? What are you trying to achieve?

mithun12000 commented 10 years ago

I am creating a theme. now default layout can be anything, but when you activate theme then it will overwrite layout to change UI.

qiangxue commented 10 years ago

You can class-level events: https://github.com/yiisoft/yii2/blob/master/docs/guide/concept-events.md#class-level-event-handlers You can attach your event handler during bootstrap of the application: https://github.com/yiisoft/yii2/blob/master/framework/base/Application.php#L173

mithun12000 commented 10 years ago

Hi qiangxue,

how can i make use of You can attach your event handler during bootstrap of the application: https://github.com/yiisoft/yii2/blob/master/framework/base/Application.php#L173?

could you explain what can be done through this and how it can be specify?

I have config some thing like:

[
    'id' => 'Test Application',    
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'extensions' => require(dirname(dirname(dirname(dirname(__DIR__)))) . '/extensions/extensions.php'),
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=tc_backend',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8'
        ],
        'authManager' => [
            'class' => 'yii\rbac\DbManager', // THIS IS YOUR AUTH MANAGER
            'db'    => 'db',
            'assignmentTable' => 'user',
        ],
    ],
];

extensions will hold component/extension configuration.

qiangxue commented 10 years ago

You need to create a class and list it in Application::bootstrap. Within this class, you can register the class-level event handler for yii\web\Controller::EVENT_BEFORE_ACTION event.

mithun12000 commented 10 years ago

sorry my question is similar but yet it is a different here i am trying to make it work this bootstrap feature.

my extension config:

'adminUi' => 
    array (
        'name' => 'adminUi',
        'version' => '1.0',
                'bootstrap' => 'yii/adminUi/AdminUiBootstrap',
        'alias' => 
        array (
            '@yii/adminUi' => $vendorDir . '/adminUi',
            '@vendor/adminUi/assets/' => $vendorDir . '/adminUi/assets',
            '@app/themes/adminui' => $vendorDir . '/adminUi/themes/',
        ),
    ),

path

[root]
|_extension
|_app
|        |application
|                          |_backend
|_framework

$vendorDir = [root]/extension

Now when i am trying to make this work i always get reflection error

ReflectionException
Class yii/adminUi/AdminUiBootstrap does not exist

but file already exist in [root]/extension/adminUi/AdminUiBootstrap also this is implemented BootstrapInterface.

may i know where is the error?

mithun12000 commented 10 years ago

also

bootstrap file def:

namespace yii\adminUi;
use Yii;
use yii\base\Application;
use yii\base\BootstrapInterface;

class AdminUiBootstrap implements BootstrapInterface{
    public function bootstrap($app){
        print_r($app);
    }
}
qiangxue commented 10 years ago

You didn't specify the class name correctly. Please read the guide first: https://github.com/yiisoft/yii2/blob/master/docs/guide/concept-autoloading.md https://github.com/yiisoft/yii2/blob/master/docs/guide/structure-applications.md#yiibaseapplicationbootstrapbootstrap-

mithun12000 commented 10 years ago

Thanks. find my error.

yii/adminUi/AdminUiBootstrap should be yii\adminUi\AdminUiBootstrap