Creating Controller and views using Gii tool in Yii2

Creating Controller & Views in YII2
Creating Controller & Views in YII2

CRUD Generator

This generator generates a controller and views that implement CRUD (Create, Read, Update, Delete) operations for the specified data model.

Click “start” button under CRUD generator to create Controller and Views for corresponding Model.

Model Class

Here you should provide the fully qualified class name of already created Model.
eg: frontend\models\Products
Note : If your model was generated in backend, put backend instead of frontend. eg: backend\models\Products

Search Model Class

Here you can provide search model class.
eg: frontend\models\ProductsSearch

Controller Class

Here you can provide controller class to be generated.
eg: frontend\controllers\ProductsController

Creating Controller & Views using Gii tool

Creating Controller & Views using Gii tool

Click on the “Preview” button to view the file path and then click “Generate” button to generate files.
possible error: Class 'frontend\models\Products' does not exist or has syntax error.
solution: Replace namespace app\models to namespace frontend\models or namespace backend\models
Now your controller file is generated in @app\frontend\controllers and view files are generated in @app\frontend\views
File structure will be like this


you can access create form through this link
eg: http://localhost/app/frontend/web/index.php?r=products/create
you can access index page through this link
eg: http://localhost/app/frontend/web/index.php?r=products/index
* app – your app name.
*products – your controller name.

You may also like...

6 Responses

  1. like to shoot you an e-mail. I’ve got some ideas for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it improve over time.

  2. Ragaffney says:

    I’d like to thank you for the efforts you’ve put in writing this blog.
    I’m hoping to see the same high-grade blog posts
    from you later on as well. In truth, your creative
    writing abilities has inspired me to get my own, personal website now 😉

  3. Greetings from California! I’m bored at work so I decided to browse your website on my iphone during
    lunch break. I love the info you present here and can’t wait to
    take a look when I get home. I’m shocked at how quick your blog loaded on my phone ..
    I’m not even using WIFI, just 3G .. Anyhow, fantastic

  4. seba says:

    I need to to show users the back-end of the system, how do i go about it, i have created view table in my xamp so i need the users to view only

Leave a Reply

Your email address will not be published. Required fields are marked *