Sergey Scopin
Sergey Scopin

Reputation: 2245

Yii2 include from vendor directory

I have yii2 basic version. I want to use Yii2 GridView. Using docs I 've installed and updated it. Now I can see directory in directory /basic/vendor directory ./kartik-v I changed it to ./kartik and it's subdirectory yii2-grid to grid Now it's said to include module in config. I go to standard path: /config/web.php and add this block as part of config:

'modules' => [
   'gridview' =>  [
        'class' => '\kartik\grid\Module'
        // enter optional module parameters below - only if you need to  
        // use your own export download action or custom translation 
        // message source
        // 'downloadAction' => 'gridview/export/download',
        // 'i18n' => []
    ]
],

How can I use it in view, for example, now? This is code of my view and it says that it can't find it there.

    use app\vendor\kartik\grid\GridView;
    $dataProvider = null;
echo GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        'id',
        'name',
        'created_at:datetime',
    ],
]) ;

Need some help. UPD1 Thanks to Tim Ogilvy I understand that I should use namespace to get it and it's not necessary to add in in config/php . Correct me if I'm wrong.

Upvotes: 1

Views: 4031

Answers (1)

Tim Ogilvy
Tim Ogilvy

Reputation: 1973

Have a read of the Yii Manual for autoloading.

It explains a couple of different ways to load classes, including using /vendor/autoload.php which is the composer autoloader.

Either way, once you have the autoloader set up correctly, you should be able to refer to the gridview via it's vendor namespace as visible in the gridview documentation.

Example taken from the Table Styling section:

use kartik\grid\GridView;

// Generate a bootstrap responsive striped table with row highlighted on hover
echo GridView::widget([
    'dataProvider'=> $dataProvider,
    'filterModel' => $searchModel,
    'columns' => $gridColumns,
    'responsive'=>true,
    'hover'=>true
]);

Note that the use statement uses back slashes rather than forward slashes.

For windows users, it's not immediately apparent that this is not a path, however linux and mac users are immediately able to identify that this refers to a namespace, and not a path.

Upvotes: 3

Related Questions