Admin panel in Yii Framework
When you are using yii framework as a developer its easy for you to access the “admin” action in GII generated crud controller. When you deleiver it to your client they mostly prefer to have a admin panel, dashboard whatever they have experienced with WordPress or other web apps. So its our responsibility to make it client friendly. To acheive this there are few methods I have tried
Admin panel in yii framework
1) Access Filters:
Using the same Controller and restrict the user roles in Access Filter for crud operation. this is the YIIs core method and when you start learning you might thought this is the only way
2) Separating the application into front-end and back end:
This is the way experts prefer for better security and most of yii starter project templates using this approach. Famous 2amigos’ yiinitializr, Yii Boilerplate, Yii App, all those templates has similiar structure. But Will tell you later why I dont prefer this method
3) Child Modules:
Here the YIIs native HMVC solution helps us to create child modules inside the top module. But you can’t generate the child module from GII. You should create it as a top level module and move it inside the another top level module and write all import stuff in module file. and its okay with Two level. Dont be greedy to have multi hirechial modules.
4) Sub Controller:
Sub controller’s are just like child modules where they are in a child folder insilde your controllers directory. I have seen few many posts in stackoverflow stating the method not working for the users. But there is a trick
I haved a module called “CMS” and i need a controller called ‘pages’ .
Generate Crud in module/cms/admin/pages. here the CMS is the module name, admin is subfolder inside your controllers directory and pages is your contoller name. If you try to access it from your browser like cms/admin/pages/admin . no it wont work.
Now create another controller in top level with same name for non admin users. Path should be some thing like cms/pages/index. Now both controller will work. . So its necessary to have controller in both folders to Yii validate the controller. You can either setup on admin theme or layout and configure it on controllers property. also you can restrict the user actions. just have the public actions in parent controller and crud actions in sub controller
I prefer Method 3 and 4 over other two methods because of the code re-usability. You can just copy paste the folders to your new project without jumping over FTP folders for each file to be uploaded.