Monday, January 26, 2015

Create module and add CRUD Operation in YII, Step by step tutorial with pictures

Modular Programming is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.
  • Go to your project or create new project
You can create new YII project by running the following code:
yiiFrameworkPath/yiic webapp projectCreationPath/projectName

  • Login to gii
For this you, first need to initialize the gii module
Go to protected/config/main.php. Scroll a bit, you will find gii module on line 21. Uncomment it

  • Go to Module Generator
Enter the Module ID. Remember, Module ID is used to name the module Class. For example, Module ID “items” is used to generate module class “itemsModule”. Now, generate the code
  • Include the generated Module Name in main.php
You can test the newly generated module by routing on the browser. Go to http://localhost/yourproject/index.php?r=items (items is Module Name). You will see ERROR 404. This is because you haven’t included the module name in configuration.
For this, go to protected/config/main.php.
Add module name after gii Module as in figure.

This is it. As you include the module name, as you refresh the path, you will see the module index.php page. You can customize and design according to your needs.

CRUD Operation in Module

CRUD inside the Module is similar to CRUD outside the module except some changes. For CRUD operation, you need to give path to the Database. 
For this got to protected/config/main.php. Find the ‘db’. Uncomment the code. Change the database name to your own Database name (“demo_tutorial” in my case) and save it.


Again login to gii (Path : localhost/projectName/index.php?r=gii/)
Now go to Model Generator. Enter your table name from database. YII will automatically take Model Class. You need to include your model path.
For including Model Path: “application.modules.moduleName.models”
Now generate the Model.
Generate CRUD
As you type the Model Class name and Controller ID and click Preview, you may see error. This is because, the newly created model hasn’t been auto loaded because it is inside the Module.

For this you need to again go to protected/config/main.php. Find the “import'=>array”Add the Model by writing below code.

‘application.modules.moduleName.models.*’,

Now generate the CRUD operation. It will successfully generate the Controller and Views.

These generated Controller and Views are outside the module. But we need CRUD operation for the Module, we created. So, The Controller file that is generated on application’s Controller Folder is cut and pasted on inside the Controller Folder of Module (Inside module’s Controller folder). Similarly, views folder that is created inside Views. So, copy and paste the newly created views folder  from application’s folder to module’s view folder (tutorial folder inside application’s  in my case) in the views.

 Still Confusion? Let’s make it clear.

Cut the “tutorial” folder from

C:\xampp\htdocs\demo\protected\views\

And paste it on the directory.

C:\xampp\htdocs\demo\protected\modules\items\views\

Similarly,

Cut the TutorialController.php file from

C:\xampp\htdocs\demo\protected\controllers\

And paste it on the directory 

C:\xampp\htdocs\demo\protected\modules\items\controllers\

Now, you can route to the modules and do CRUD Operation by navigating through:
localhost/projectName/index.php?r=moduleName/controllerName/index
In my case, moduleName is “items” and controllerName is “tutorial”.
In this way, you can create module and do customize as your need.

No comments:

Post a Comment