Extend kartik GridView widget class - Advanced application yii2

559 views Asked by At

I want to create a new class that extend from yii2 Kartik gridview

namespace mywidget\grid;

use kartik\base\Config;
use kartik\dialog\Dialog;
use kartik\mpdf\Pdf;
use Yii;
use yii\base\InvalidConfigException;
use yii\bootstrap\ButtonDropdown;
use yii\grid\Column;
use kartik\grid\GridView as YiiGridView;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\JsExpression;
use yii\web\View;
use yii\widgets\Pjax;
class GridView extends YiiGridView
{

}

the problem is when i call grid view , an error exception is thrown : Class not found. So i wonder if it'is the right way to extend from a widget class ??

2

There are 2 answers

0
csminb On

the exception is about my class which is not found : use mywidget\grid\GridView

it sounds like it's an autoloading issue, make sure the file you're working in has the same path in your project as the namespace you're using. in this case should be mywidget\grid\GridView.php or adjust your namespace to match your file location

.. if that's not the case please provide more details about the error you're receiving and use case of your class

2
Bizley On

The namespace you have used is not registered.

The simpliest solution is to change the namespace to one of the registered with Yii 2.

  • for Basic Project it's app - so if you the path to your extended class is mywidget/grid/GridView.php namespace is app\mywidget\grid
  • for Advanced Project it's common, frontend or backend so depending on the one you choose place folder there and replace app accordingly

If you insist on using mywidget\grid namespace you have to register it first. Read more about this in the Guide: Class Autoloading