mvc controllers (suitable approach)? - php

Even after studying from many resources on the internet, my concepts of controllers are still weak.
Currently i am making a simple website with Laravel framework with an admin integration and home page.
Posts are being rendered on the home page and in admin panel too.
Admin can create more admins and posts.
I have 2 models (users, post) and 2 controllers HomeController and AdminController.
Now where should the Create Post function go?
I mean should i create 3rd controller (PostController) for showall, showsingle, create, update and delete Posts?
My AdminController is already messed up with functions of creating, updating and deleting admins. What should i do and why?

Related

CakePHP: Make Dashboard page by get data from other Controllers

I have researched for this solution all month but I didn't get the right answer for my page. Please take a look and give me suggestions.
I have the admin page and the view is Dashboard.
I have tables are Staffs, Customers, Comments, Contacts....
I created these page to show data on view successfully for each of them.
NOW, I want to make the dashboard page to view each 5 top new Staffs, Customers, Comments....were being added to system. The summary page. My Dashboard page will be my main page (when access the domain, it will direct to dashboard page).
My question are:
Where should I create the Dashboard page in?
How can I take the data from Staff Controllers, Customers Controller....in the dashboard page. Dashboard page is not include in view of these above controllers.
I use cakephp 1.3
Please give me the best suggestion for this. Thank everyone much !
First of all it depends upon the way you wanna go ahead:
1) Where should I create the Dashboard page in?
Its totally upto you where you want to create the page, but it should ideally be the page to be redirected after the admin login.(BTW its a foolish question to be asked.)
2) How can I take the data from Staff Controllers, Customers Controller....in the dashboard page. Dashboard page is not include in view of these above controllers.
You can do that in 2 ways:
a) Create common functions for these in their respective model and just call those into your single function created for dashboard.
b) Create functions in their respective controller and import the controller and use these methods in the dashboard function(But avoid this approach as long as you can as it will load unnecessary controller class files).
Hope it helps you.

Is it possible (and how to?) divide application to backend (admin part) and frontend (user part) parts?

I am developing web application, using zend framework 2. In my app, I want some modules. News, for example, users etc.
Is it possible, to divide whole application to two big parts: front-end (for users) and back-end (for admins).
In front-end there will be part of News module, which render news list, full news item etc.
In back-end there will be part of News module, which will render list of all news, tools for editing, deleting etc.
At the moment, I have simple structure:
I have MyZfcAdmin module, with routes configurated so, that when I am writing /admin/news/edit/5 it will call NewsController edit action with id 5 and render it. So now, I have all actions (for admins and for users) in one controller. Is it possible to create something like this:
module
frontend
News
SomeMoreModule
backend
News
SomeMoreModule
I found topic on google questions, in that suggested to use this CMS, but I want to try it by myself.
Different people have different idea in managing application, for me I will have news module that handle all news related calls both admin parts and user accessible parts. The idea you have described have some side effects like duplication of code between modules and crossing of borders between isolate modules like calling action in another modules(it's ok if there are related or fall in hierarchy), which may have issues in long term maintenance of the applications.
IMHO I might suggest you could create modules like News, users, etc where it can have combined functionality of both admin and user access. You can later include user authorization plug ins using bootstrap events and restrict action accessible by admin and other users
There is not concept of submodules inside any module. But you can give them proper naming conversations. If code is not formed in oops properly, then there can be code duplication into your project. As per #raj's answer, it's good to give restrictions based on user role. If any action is not intersecting admin and user role then you can separate those actions, but actions like add or edit news content are same for both role then you should point to same action by router.

Location of admin controller methods

I am building a Laravel 4 application and I am trying to sort out where my admin controller functions should go.
eg my admin user view, edit and update functions.
Previously in my User controller, I would have
function getIndex()
{
// Get method for normal users
}
function getAdminIndex()
{
// Get method for admin users
}
I would then have in the routes /users -> getIndex() and /admin/users -> getAdminIndex()
however, is this ideal?
The reason is it makes my routes file quite large as I have to specify every route.
With things like Blog posts, and Products, should I have one controller for logged out / user access, and then a separate controller, in an admin folder for just the admin functions?
Is there some open source projects I can look at?
You should separate your regular controllers from your admin controllers. It's a matter of personal preference how you do it: you can create folders inside your controllers folder, or maybe you could create –almost– independent modules with controllers on their own.
For the first option, take a look at this project:
https://github.com/andrew13/Laravel-4-Bootstrap-Starter-Site
For the second one, which is a bit more complex, you can get inspired from this post by Ryan Tablada:
http://ryantablada.com/post/juggling-larger-laravel-applications
I personally prefer this last one if the project is medium/large size, and the other one if it's a small project.
You can also try Laravella, a CMS, CRUD, Bootstrap, Uploader etc. framework for Laravel.
https://github.com/laravella/laravella/releases
If your project is small, then you can use Resource Controller.

CI and HMVC structure

I’ve to make a webapp with public and administrator parts and we are two different developer teams. I’ve searched the best way to do that and I found HMVC. I accomplished to install the codeigniter-modular-extensions-hmvc from wiredesignz and it’s working, but I think I haven’t understood the philosophy of the folder/module structure.
If I want to create a webapp with admin and public pages referencing the same db tables (for example with the admin part can configure the general preferences, blog categories, add users, add roles… and with the public part an user can add blogs, images, ...)
It will be a good structure something like that?
/modules/blog/ -> only blog model
/modules/user/ -> only user model
...
/modules/login/ -> login controller and pages referencing user and role modules (models)
/modules/admin/ -> admin controller and pages referencing other modules
/modules/public/ -> public controller and pages referencing other modules
...
/modules/templates -> controller and pages for public and admin template
If I want to have more than admin or public controller, I’ve to create more modules? (modules/admin_dashboard, modules/admin_users, ...)
I hope you can help me, I’m a little bit lost :/
Thx!
There are some nice tutorials by David Connelly on youtube. He suggests '1 table - 1 module' structure. Then, you can use those module's controller's methods in other modules (like dashboards) to get or manipulate the data. This is how I understood it.

Best way to make Admin pages in CodeIgniter?

I'm working on an app in CodeIgniter, and I want to have admin pages for several of the objects in the application, and I'm wondering what would be the better way to put these into an MVC structure.
Idea 1:
In each controller, have an admin function, and add all of the admin pages I would like into that function.
example URL: domain.com/articles/admin
Idea 2
Make a new admin controller, which would have to reference many different models, and put all of the admin pages in there.
example URL: domain.com/admin/articles
Which way would be better?
Edit for clarification: By admin functionality, I mean being able to do the basic CRUD actions on any object, and be able to display a list of all of said object.
Definitely a different controller at least!
I used to think that I could keep all my admin functions in a single controller, but as my programs grew, I realized that I needed multiple controllers in my administration section.
So, I created a folder inside my controllers folder with the name "admin" and put all my administrative controllers in there. So my folders would look something like:
application
controllers
front.php
welcome.php
admin
dashboard.php
useradmin.php
etc...
One problem this creates, however, is when you type http://mysite.com/admin in your browser, it returns a 404 page. So, go to your "application/config/routes.php" file and add a custom route:
$routes['admin'] = 'admin/dashboard/index';
I'll echo Justin in keeping it part of the individual controllers.
You should setup some kind of authorization system that the individual controllers can use to so who is logged in (username) and what access they have (admin/member/etc). Here's a SO thread on CodeIgniter Auth Classes.
The view would then conditionally show the appropriate links, and the controller would enforce the policy by checking the auth before passing any data to the model or rendering an edit view. On unauthorized access an error could be rendered, or simply render with the non-editing view.
This approach seems to make the most sense (at least to me) because all the functionality is stored in the individual controller. Keeping admin functions in a single admin controller means you'll have to manage two controllers (the admin, and the actual controller) every time you add somethign new (or remove something).
If you're concerned about putting auth checking in every controller, you could create a generic controller class with all the auth setup, then have your controllers extend it. In the end the individual controller auth check could be as simple as:
function edit()
{
if(!$this->auth()){
//display auth error, or forward to view page
}
}
Of course some kind of ACL implementation would make this better, but I don't believe CodeIgniter has an 'official' ACL.
It's a good idea to have an admin folder in the controllers folder wherein you can access your administration e.g. yoursite.com/admin/users.
All your administrative needs will be there and all methods will be protected by checking user privileges like so:
if ( ! $this->auth->logged_in(array('login', 'admin')))
{
$this->session->set_flashdata('message', 'You do not have access to view this page');
redirect('admin/users/login');
}
Then all controllers outside the 'admin' folder will - depending on your type of site - will only be for viewing, etc.. no administrative portions.
Idea 2 is better.
system/application/controllers/admin
You keep all your admin controllers here.
Here is an extensive guide to the pro's and con's of each method:
http://philsturgeon.co.uk/news/2009/07/Create-an-Admin-panel-with-CodeIgniter
Depending on what you mean by 'Admin' functionality...typically, this is thought of as an 'Edit' view.
And typically, you use the existing controller to serve the 'Edit' view allowing the authorized users to make the edits (in your case, Admin users only).
Looks like a personal choice, i love having everything centralized so the admin controller would be my bet.
That way i wouldn't have to open up 5 different controllers while modifying admin tasks.

Categories