Zend Framework :: General App Design Question - model-view-controller

I just started learning Zend (& OO PHP for that matter), I have spent the last 4-5 weeks learning, tutorials, books etc. I feel good about it but will bog down in the models (thats ok, I'm learning). I am now beginning my first app (for work even); It has at least 5 major sections (including the login + will need ACL), and a couple will have up to 10-12 sub sections like admin: create user, edit user, etc.
I created a single layout, and have made most of the page views with working links, and have a few of the forms complete already.
My major concern now is should I refactor and make modules of the major sections before it gets out of hand, or am I worried about nothing. One thing I think I did wrong is that I have a 'AdminController' that does nothing but bring in the admin 'view' that is nothing more than links to each 'user' action in the 'UserController'. I'm thinking maybe I should have put the user actions in the AdminController. I'm thinking too, that I should make a 'admin' module, 'reports' module, 'auth' module, etc. Or is it normal to end up with 8 controllers and growing? I already have the inclination to make and maintain a developer's sitemap just for my own sanity, not to mention that I want to do the best job possible :)

In principle, I like the idea of a plugin-able module for each set of functionality - News, Users, Galleries, etc. "Plugging in" that module would provide functionality for the back-end admin and the front-end display. It is a self-contained place to put all the functionality - models, action helpers, view helpers, view scripts. etc - that you need for that content area. There might be two controllers per module - News_BackendController and News_FrontendController - dedicated to their specific areas.
But in practice, I find that ZF modules make that hard. I know that smarter guys than me - a low bar, to be sure - can make it all work, but I've never had luck with it.
So I usually end up with two modules - frontend and backend. For news functionality, for example, I'd have a news controller in the backend module for managing the content; another news controller in the frontend module for displaying it.
The sticky point for me in this setup is where to put model functionality that is common to both frontend and admin. One idea is to put it out a separate library and then create module-specific models that extend these for any module-specific functionality. Something like:
MyLibary_Model_News for the common news stuff.
Frontend_Model_News extends MyLibrary_Model_News for any frontend-only news functionality, if any.
Admin_Model_News extends MyLibrary_Model_News for any backend-only news fnctionality, if any.
Just some ideas. As always, YMMV.

Related

There's any issue if I don't place my models into a specific folder inside the app directory?

I am following a tutorial where the tutor places all of his models inside the following path app>models.
I'm not willing to do that, but he is always talking about best practices like the one I'm talking about.
So, there's any problem?
Even though Laravel is a MVC framework (Model-View-Controller), we don't see a folder called Model in L5 and above.
People often tend to add everything in a folder called Model just as to group all models together, but it's optional in L5 and above.
But yeah, it's always good to keep everything under a folder called Model in my opinion.
Laravel just doesn't bottleneck you or your project into using any one method. This is something for you (and your development team, if you're a part of one) to decide on. That is the convention behind Laravel - giving you the developer choice in the matter.

Codeigniter - a dynamic sidebar

I only started with this a couple of days ago, so this may be a silly question...
I have an admin area with its own directory "admin" within the controller folder. So if the user is in the admin area, I want a sidebar to show. But obviously there will be a fair few "pages" (controllers) within the admin area. I have Clients, Services and Dashboard.
In the sidebar (on all pages) I want a list of clients and services so when clicked, it goes to a page and display info for that client/service.
I sort of have it working with add_service(), edit_service(), view_services() etc... but in each of these methods, it seems like I need to load services AND clients models, and pass the data back to each view... in all methods? So if I want to add a service, I click "add service" and it takes me to the add_service(). Do I need the below 4 lines in each method?
$this->load->model('client_model');
$this->load->model('services_model');
$data['clients'] = $this->client_model->get_clients();
$data['services'] = $this->services_model->get_services();
I have read about widgets, but not sure if that's what I need exactly.
Thanks
As you read, all you need is HMVC. This will create a "widget" with your controller, model and views, and you'll be able of calling "a whole": A part of the website wich manage its own part.
One solution is implement https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc into your code, you create a module called "client", and in your "admin" controller you call it. You'll get your client table there, and in your controller "client" you'll be able of call it too, and you'll have exactly the same behaviour. I've implemented wiredesignz HMVC quite times and I think is the solution for your trouble. Anyway, you could also check https://github.com/Bigwebmaster/codeigniter-modular-extensions-hmvc, which is a fork with improvements into the code.
About your sidebar, you'll have to call the widget you'll create with your clients, and you'll have it quickly with only one call, because the module will manage all the behaviour

wordpress plugin MVC page structure

I'm working on a wordpress plugin that takes the whole page and is about planing a trip.
The plugin spread out and contains several modules and more than 10 "views" (Booking, Billing, Register, My Profile, My Bookings, My comments, etc.).
I have a strong OOP and MVC background but this plugin was originally created on a single template with everything loaded with ajax and not in a organized way :)
What is good practice for organizing big plugins (semi small sites) in wordpress?
Is there a way to create direct links for the modules view files? something like:
domain.com/blog/wp/plugins/my_plugin/profile.php
Bounty:
I'm looking for guidelines here from people with experience.
Firstly, it's not easy to do, and it's harder to stick to as development progresses.
I'll try to answer your bullet points first, then try to talk about some architecture stuff I try to adhere to.
I organize them as close to normal as possible. So I usually end up with folders for Models, Controllers, Views. Try to write your application as much as possible in the same manner you would for anything else.
Use plugin_url().
If you're working on a project that won't be distributed (i.e. not a publicly released plugin), you've got some advantages because you can load in outside packages from Composer without worrying about conflicts from other places. So wherever possible, I'd advise offloading stuff to Composer.
I'm not a fan of how PHP implemented Namespaces, but I'm a huge fan of using them in conjunction with autoloading. You'll definitely make things easier on yourself if you use some form of Autoloading, even if it's not used with Namespaces.
Since WordPress works off of functional hooks, unless you (over?)engineer a lot of stuff, you're always going to end up with a bunch of hooks all over the place. Generally, my advice there is to try and keep those together in a file, and to never put hooks inside classes, especially constructors. Keep stuff in logical groups.
The trick really is to minimize the number of points where you're actually interacting with WordPress, and everywhere else to essentially write your code as you normally would, with decent design patterns and the like. You'll have to have certain points of contact (like the hooks and such) where you'll probably find yourself making some concessions to WordPress, but even there you can mitigate it by loading object methods as hook callbacks, and using those as jumping-off points to a "normal" application.
I've been interested in this problem for a while. I've got a couple ongoing projects in this area. One thing I threw together was for interfacing with GravityForms, and it's on github. It's really not complicated, but it might help explain some of how I've got about solving the problem.
I'm running out of specifics to add, but please feel free to drop me a line if you like. As I said, I'm really interested in solving this problem, and I think that if WordPress lasts and continues to be as popular as it is today, we'll have better solutions coming around.
I hope this is helpful!
EDIT: A more concrete example
I'll point some stuff out in the code I shared originally. It's sort of specialized, but you can use the principles for any hook-based functionality. As you can see here, I'm invoking a method of class GravityFormsHooks\Loader to handle hooking into objects. In GravityFormsHooks\Loader, I'm calling another static method on that class to actually execute the hook. This example will take either an action or filter, but it's tailored to Gravityforms specifically, so YMMV.
Essentially what this GravityFormsHooks\Loader::hook() method does is instantiate the class we're hooking into, and generate the hook as normal.
The class I'm calling from the main plugin file is GravityFormsHooks\Forms\Form. Note that any method you hook onto MUST be declared as public. If we're going to shoehorn ourselves into an MVC paradigm, this method here would be your controller. From there, you can jump off to injecting models, a template engine, all manner of cool stuff.
As I mentioned in my original post, I try to keep my points of contact with WordPress to an absolute minimum. I don't mean that you should write APIs doing stuff WordPress already has APIs for, just that your hooks should be centralized and minimized. It really is a useful separation of concerns, and as your application grows it'll help you to manage the complexity a lot more easily.
The examples I provided should work pretty well as a Hook Controller, with minimum modifications to remove some of the more specialized GravityForms stuff.
Let me know if you've got any other questions.

CodeIgniter - where to put CMS

I come from a background of coding my client website projects from scratch with MySQL and PHP. They are typically public facing sites with a mixture of static content, dynamic content plus a little functionality here and there (i.e. account, searching etc). Nothing too heavy.
So I generally code up a little CMS site to let them update what they need to, put it in a protected folder and that's that.
Now I'd like to use CodeIgniter and more of a MVC approach for my next site. Should I be doing anything to separate the CMS out from the rest of the site or is it simply another area (with its own controllers/models etc) and extra authentication?
There are several good methods to create an admin interface in CodeIgniter described at:
http://philsturgeon.co.uk/news/2009/07/Create-an-Admin-panel-with-CodeIgniter
You probably will want to look mostly at "#2" on his list; it's lightweight and not much additional work.
The CMS is part of the site, if by CMS you mean administration panel / backend of your application, you can use the above link provided by coreylink as it details different ways to create an admin panel.
My personal preference is to create a folder called 'admin' in my controllers directory and create semantically meaningful names so my urls are webapp.com/admin/users and so on.

Is WordPress MVC compliant? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Closed 7 years ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
Some people consider WordPress a blogging platform, some think of it as a CMS, some refer to WordPress as a development framework. Whichever it is, the question still remains. Is WordPress MVC compliant?
I've read the forums and somebody asked about MVC about three years ago. There were some positive answers, and some negative ones. While nobody knows exactly what MVC is and everybody thinks of it in their own way, there's still a general concept that's present in all the discussions.
I have little experience with MVC frameworks and there doesn't seem to be anything about the framework itself. Most of the MVC is done by the programmer, am I right? Now, going back to WordPress, could we consider the core rewrite engine (WP_Rewrite) the controller? Queries & plugin logic as the model? And themes as the view? Or am I getting it all wrong?
Thanks ;)
Wordpress itself is not architected in MVC, but one can build very MVC oriented themes and plugins within the framework. There are several tools which can help:
WordPress MVC solutions:
Churro: # wordpress.org/extend/plugins/churro
Tina-MVC: # wordpress.org/extend/plugins/tina-mvc
Plugin Factory: # wordpress.org/extend/plugins/plugin-factory
MVCPress: http://mozey.wordpress.com/2007/01/22/mvcpress-screenshots/#comment-3634 (abandoned, but interesting ideas)
MVC threads on WordPress.org Ideas and Trac:
http://wordpress.org/extend/ideas/topic/mvc-plugin-framework
http://wordpress.org/extend/ideas/topic/complete-reestructuring
http://wordpress.org/extend/ideas/topic/rewrite-wordpress-using-mvc
http://wordpress.org/extend/ideas/topic/wordpress-theme-revamp (more on XSL than MVC)
http://core.trac.wordpress.org/ticket/12354 (on MVC in widgets)
Wordpress is kinda-sorta MVC. If anything it is a pull-type MVC layout, where the View 'pulls' data from the model. It does this in a very proceedural way, instead of using lots of different objects, but this actually makes the front end templates easier to write in a lot of ways.
This also gives the views some degree of controller logic (thus the kinda-sorta MVC).
Lets run this down:
Wordpress gets a URL. The wordpress core acts as a controller and determines what initial queries to run of the database, and by extension, what view should be loaded (category view, single post or page view, etc). It then packages that INTIAL query response and sends it to the view file.
That view file CAN be a strict display only file OR it can request additional information/queries beyond the built in one. This is the pull-type of the MVC, where the view pulls data from the model instead of the controller 'pushing' data from the model into the view.
Thus, when the view sees code to load a sidebar or widget area, it asks for that information. However, what widgets should be there is determined by the controller, which looks at the model for what widgets are in the sidebar, and then selects those that are set to show on the current page, and returns those to the view.
That each part of that isn't an object doesn't make this any less MVC. You can alter WP core without (necessarily) altering anything about a theme. Similarly, as long as you use built in functions like 'get_pages()' then the model and the database tables could change as long as those functions still returned the right data. So, the model is independent of the view, and the controller is independent as well (except when the view adds controller logic to do more than the core normally does).
While you COULD have a model object holding a number of methods and stuff like WPModel::get_pages('blah blah'), and contain everything that way, there is still fundamental separation of concerns.
View: template files
Controller: WP core
Model: the various functions that handle specific data handling.
As long as the names, arguments, etc, stay the same (or just have new ones added) then separation of concerns is maintained and one can be altered without disturbing the others.
It isn't a super-clean version of MVC, (especially when hooks get involved), but at a basic level it starts there.
And being proceedural about it isn't a bad thing IMO. A request from a website is pretty inherently proceedural: it is a process with a clear beginning and end, and just needs a procedure to process the request, get data, package it, then die. You can set up those steps with objects and object methods and OOP layouts (which would make some things easier) or you can just write alot of function calls and separate them out that way. Class members like private variables are lost that way but depending on the needs of the application... you might not care.
There is no one-grand-way to do development, and WP sits at like 20% of websites so it is doing something right. Probably something to do with not making people have to learn/memorize complex class hierarchies to get the database to answer the question 'what pages are child of page x?' and deal with that data. Could you make it that easy with OOP? yes, but if Joomla is any example of how hard it is to implement a complex custom website with OOP, then WP is FAR easier and quicker, and time is money.
As already mentioned in the comments, MVC is an architectural design pattern, not a specific framework, and no, Wordpress doesn't follow the MVC pattern.
There is a separation of views (templates) from the programming logic, but only in the frontend, not in the admin panel and a general separation of views and application logic is not inevitably MVC. An implementation of the MVC pattern usually assumes some kind of object oriented programming paradigm behind it and Wordpress is mainly implemented in a procedural way, with plain SQL queries in the PHP functions, therefore not having an actual model either.
One of the topics that periodically crops up in discussions as it relates to WordPress is the idea of WordPress and MVC.
But the thing is that MVC is not the silver bullet of web development that we try to make it out to be. Yes, it’s an awesome design pattern, and I personally think that it fits the web application model like a glove, but not every framework or platform implements that design pattern.
Case in point: WordPress is not MVC.
And that’s okay. I think we need to leave the desire of trying to shoehorn it into our projects aside especially when the pattern WordPress provides is not only sufficient, but works well when leveraged correctly.
“But I Love MVC!”
So do I! In fact, I spent the last year working on a project that more-or-less mimicked the MVC architecture.
A high-level example of MVC.
A high-level example of MVC.
For example:
Views were implemented using templates
Controllers were implemented by a combination of using function names like create, read, update, destroy, delete, and so on (even though these functions were hooked into the WordPress API
Models were functions also were called to validate and verify data prior to serializing the data. Again, this required that certain functions be hooked into WordPress to achieve the desired result.
Finally, a set of rewrite rules gave the application a clean set of predictable URLs in the format of /people/update/1 or /people/all.
What Pattern Does WordPress Implement?
WordPress implements the event-driven architecture (of which there are several variations such as the Observer Pattern).
In short, you can conceptually think of this as the following:
Things happen when WordPress is processing information.
You can register your own function to fire when these things happen.
Not too complicated, is it?
A high-level example of event-driven patterns
A high-level example of event-driven patterns
When you begin to think in terms of the paradigm in which it works rather than trying to make it work the way that you want it to work, it’s liberating. It helps to solve problems much more easily.
The bottom line is this: WordPress implements the event-driven design pattern, so even if you end up trying to implement MVC, you’re still going to have to utilize the hook system.
If you’re not careful, you can end up trying to craft the perfect architecture without actually getting your work done, and thus end up finding yourself so high up in the atmosphere of software that you’ve effectively become an architecture astronaut.
So You’re Saying Avoid Design Patterns?
Not at all! Design Patterns serve a purpose because, above all else, they basically give us solutions to previously and commonly solved problems. Use them!
But the point I’m trying to make is that we don’t need to try to force things to fit pattern just because we like the pattern. That’s not their purpose. Instead, leverage the primary pattern that your platform of choice implements – in our case, it’s an event-driven pattern – and then implement patterns where they fit (such as dependency injection or something like that).
Otherwise, it’s like trying to put your foot in a glove.
Courtesy (and totally copied :P) from : http://tommcfarlin.com/wordpress-and-mvc/
Just to update this with more recent information for people hitting this from search engines - the wp-mvc plugin http://wordpress.org/extend/plugins/wp-mvc/ goes a long way to creating a mvc framework for plugin development. You can find out more here: http://wpmvc.org/documentation/70/tutorial/
Just to add to the list of options, (I'm admittedly biased as the author,) swpMVC is a fully featured, lightweight MVC framework, inspired by Rails, Sinatra, Express, and FuelPHP. It's thoroughly documented, and while I have used and enjoyed wp-mvc, I wanted something where the models were able to populate views themselves, including form controls for interacting with said models.
I put this together largely to reduce the amount of controller code required to put together an app on top of WordPress, and the result is a very fast and effective framework that runs inside WordPress. The models are based on PHP Activerecord and 8 models are included for existing WordPress data types, including Post, PostMeta, User, UserMeta, Term, and a few more. Modeling data is very easy thanks to the activerecord library, and I've enjoyed working with this framework immensely thus far.
Also ships with underscore PHP and PHP Quick Profiler (as seen in FuelPHP.)
RokkoMVC is a micro MVC framework built especially for WordPress. The project is meant to simplify AJAX functionality in WordPress applications, as well as bringing in all the other benefits of using models, views, and controllers to your theme.
I had a bash recently at creating a plugin that makes use of a simple view-controller system, and quite liked the results, so I separated the template stuff out to its own repo. It offers object-based controllers, passing variables locally to PHP templates, template fragments (templates within templates) and components (template fragments with their own sub-controller). All in two tiny classes!
Of course, I wrote this code thinking that no other WP developer had considered the problem before ;-).
It's far from mvc, there is no kinda-sorta thing like some people say, it's either MVC or not... The fact that you write logic on the view level doesn't qualify it as a mvc framework. The reason people use it - it's easy to learn, you don't need to be hardcore php programmer, they're lazy.

Resources