[wp-hackers] Options for Controller - Views

Jacob Santos wordpress at santosj.name
Fri Nov 20 15:08:34 UTC 2009

Yes, it is completely possible to use the template redirect hook as the 
controller, but in this instance the reason is that you would have to 
have the majority of your controller logic in there or abstract it out 
into separate functions to keep the function from becoming massive. So 
if I did the extra work and allowed for the Rewrite to call the function 
directly, I don't have to worry about all of the logic myself or I could 
have a mile long function for all of the points of the project.

I will take for instance one project where I replaced two sections and 
provided for two other sections. Yeah, I didn't use the add_rewrite_rule 
and I may add it in there just to see how well it works and if it 
removes the need for the title hack.

MVC as a pattern does extremely well in large web based projects. I 
mean, extremely well, because you know exactly where everything is. All 
of the views are together (like in WordPress), all of the models are 
together, and mostly all of the controllers are together. You know 
exactly where to look, if I'm building the view data, then I'll go to 
the controller. If there is something wrong with the data, then I'll go 
to the models. If there is something wrong with the design, then I'll go 
to the views.

As an aside, the Zend Framework also allows for regex rewrites, so it 
isn't completely limited to the structure they place you in. It also 
supports the same kind of system as WordPress. I'm not saying we use it, 
because there are lighter weight ones that can be used.

And yes, you are correct, I could start with a framework and pull out 
the parts that I need. My project is going to be GPL, so there won't be 
any license conflicts, so I could just pluck the library out of 
WordPress and recreate the administration. The reason I want to use 
WordPress is that it is GPL, has many plugins that the person who uses 
my project will have access to immediately. The majority of the code 
base for authentication, authorization, and security is maintained for 
WordPress, so if I recreated everything, I'll have my work cut out to me 
with regards to security. If there is a defect, when will I know and how 
long would it take for me to fix it. Sure, WordPress appears to have one 
every 6 months, but most of them are extremely hard to exploit at this 
point. I'll be taking the risk, even with all of the precautions I'd 
take to prevent exploits.

How this benefits me is that there is already a structure in place, sure 
it may take a week to three to rewrite the WP_Rewrite, but alas if it 
isn't wanted in the core of WordPress, and since I'll technically be 
forking WordPress (like WordPress Mu was a fork), then I can do it and 
maintain it. I would just rather put it back into the community. What 
I'll be saving is the days or weeks it will take to create the themes 
code, the plugin code, the administration. It will allow me to spend the 
majority of my time working on my project and completing it in short 
order verses spending that time recreating what I need.

I think what I'm going to do, is write it for my needs, create the patch 
and see where it goes. Who knows, if Mu or bbPress has any controller 
logic, it may end up in WordPress at some point. It could even be better 
than my implementation.

Alas, I believe I went too far in to why I need it and lost the point. 
The point is that WP_Rewrite should not be doing the mod_rewrite and the 
controller, I think the programmer that architect the code thought, 
"What better rewriter is there other than mod_rewrite? The answer, none. 
Lets use it and force the person to create other files for their 

What I plan on doing is splitting the WP_Rewrite out into the 
mod_rewrite reader / writer and the Router. It won't be perfect, but I'm 
sure I can put in some extra code for WP_Query to ignore placing 404 
when there is a match in the Router. In effect, really, I will be 
placing the Router before the WP_Query and the WP_Query will be created 
based on the controller. So it might not be as ugly as I thought and 
shouldn't be that much work to fit it in to WordPress core.

Well, I know my plan and the developers with commit access have not yet 
chimed in to the discussion, so there I don't have any idea if it will 
be accepted. I believe that as long as it doesn't break too much code 
and plugins that it will probably be accepted, unless code already 
exists in one of the other projects.

Jacob Santos

Otto wrote:
> On Thu, Nov 19, 2009 at 9:06 PM, Jacob Santos <wordpress at santosj.name> wrote:
>> Using template_redirect is a dirty hack...
> How is template_redirect a dirty hack? The template-loader is the
> entire "view" portion of WordPress, and IMO it works just fine. You
> can select your "view" based on any criteria you prefer. yes, it's one
> big set of else-if's, but the template_redirect lets you add your own
> set of criteria and to bypass the existing templates (views) entirely.
> I think you're too wedded to some pre-existing other concept of MVC
> that you have instead of working within the existing framework.
>> and I can get by not using
>> add_rewrite_rule function completely with a few other hacks. The point of
>> MVC is to easily extend the base with a defined flow path. Right now it is
>> like a maze with many paths leading to dead ends.
> I don't see that at all. It's a very clear and well-defined structure,
> albeit a potentially poorly named one.
>> In fact, I could do without the whole "blog" portion and use the Plugin API
>> and other parts, like the administration to create something completely
>> different using WordPress as the base.
> The administration piece of WordPress is the real dirty hack here. Not
> to disparage anybody, but it's a total mess. Dozens of points of
> entry, cross-including all over the place... it's extremely difficult
> to work with. The front-end portion is trivially easy to extend by
> comparison.
>> As it stands now, it would require a
>> great deal of hacking and I could never really remove the portions of
>> WordPress that makes WordPress, well WordPress. If that is the case, then
>> I'm better off starting from a framework that actually implements MVC
>> properly and without all of the WTF?s that exist within WordPress.
> Well, yes, basically you are. You're talking about wanting to make
> WordPress into not WordPress, and in that case I'd ask why bother with
> starting with WordPress in the first place? I've used the Zend MVC
> framework before, and it's quite good. If you want specific portions
> of WordPress, then quite a lot of it has been abstracted out into
> classes. Things like the Plugin API and such are actually somewhat
> trivial to recreate, because lets face it, all that really does is to
> keep a dynamic list of files somewhere and then includes them, one at
> a time. Creating an interface to do that is not difficult. The clever
> bit there is scanning the plugins for the header information.
>> Also, it has to be easy, so if someone wants to extend what I created, they
>> can do so without having to kick their own ass trying to figure it out. Do
>> you know how long it takes to learn Zend Framework, Code Igniter, etc? An
>> hour and it is pretty straight forward with all of the examples and that is
>> also with working development. I spent 4 hours trying to use WP_Rewrite and
>> after feeling like an idiot and cursing the name of the person who created
>> such a monstrosity, I gave up and used the template_redirect route. To find
>> that I have to use it anyway isn't all that appealing.
> I'm confused as to what exactly you were trying to do with WP_Rewrite
> that you found so difficult. Calling add_rewrite_rule to, well, add a
> rewrite rule seems fairly straightforward to me. WP_Rewrite is just an
> internal implementation that is basically equivalent to the .htaccess
> rewrites method. It doesn't claim to be much of anything more. It
> doesn't connect URLs to "views", it changes URLs from one form to
> another. That's all it ever claims to do. If you want to connect query
> variables to views, then template_redirect is the right way to do it.
> That's not a hack, that's by design. The whole thing is based on query
> variables, WP_Rewrite just lets you create those variables based on
> other characteristics of the URL.
>> I think the last time I tried this I attempted to work WP_Query in the
>> project as well as it was far too much rewriting. The goal should be to get
>> closer to an simpler MVC implementation.
> I disagree. The goal should be to abstract the pieces into separate
> components, yes, but not to go full MVC with all the limitations that
> encompasses. MVC can be nice for some things, but it is ultimately
> frustrating and limiting when applied to Web Applications.
> To pick an example, Zend MVC is relatively easy to work with, but it
> ultimately limits you on how you can construct your URLs. It's
> inflexible in several respects because of the assumptions it makes
> about how you want your URLs to look. WordPress' rewrite system is
> highly flexible, since you can make your URLs look any way you can
> express with regexp, then change them into query_vars, and direct to
> your templates based on those vars.
> -Otto
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list