Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter versus my homebrew MVC solution
#1

[eluser]Andrew Vit[/eluser]
Hi all,

Last year I created a lightweight MVC framework for my projects that need to be hosted on PHP4 servers, following a philosophy similar to CodeIgniter (or Rails actually, as closely as could be emulated in PHP4). CodeIgniter is more feature-rich than what I've developed for myself, so rather than maintaining my own, I'd like to know if it makes sense to throw my hat in with the CI crowd...

First, I need to investigate if it's feasible for me to migrate my stuff to CI. It seems like my sites could convert quite easily (hey, it's MVC after all... well structured and separated!), but I'd like to know if there's a built-in or straightforward way to do a few things that I depend on heavily.

My core functionality automatically displays files from the views folder, if they exist in a subfolder named according to the controller:

1.

GET /articles/howdy (which calls up "articles_controller") would automatically display the file views/articles/howdy.html, unless I define a function named "howdy" to override this.

2.

If a file under views/layouts/application.html exists (or one more specifically named after the controller like "articles.html"), the view file gets wrapped in a layout template automatically.

3.

My layout templates have placeholders for different parts (stylesheets, scripts, etc.) and these are automatically inserted as well, based on the existence of specific filenames:

stylesheets/articles.css (For any view in the articles controller.)
stylesheets/articles_howdy.css (Loads for the articles/howdy view only.)

The above allows for a loose sort of "CMS" where the content can be updated easily by editing the (mostly) plain HTML "partial" view files. Anything can be overridden, but the key for me is the default behaviour so that nothing needs to be defined in PHP functions unless needed for specific behaviour.

I'd appreciate if you could describe how something like this could be done in CodeIgniter.
#2

[eluser]NemetraL[/eluser]
I suggest you to look for Coolfactor's view library.

He basically deals with the Views through a separate class; within this class, you can define the convention tips you figured above.

As usual, a library is just a class to drop in the application/libraries folder and to be loaded through
Code:
$this->load->library('library_name');
within the main controller.
#3

[eluser]Andrew Vit[/eluser]
Thanks for the tip.

To be honest I haven't looked at CI again since my last question, but I don't think the View library is quite what I'm asking about.

I think View is probably good as a templating solution (either that or YATS), but what I'm wondering about is a way to perform something similar to "method_missing" in ruby: a way to define a default behaviour for undefined methods. I would want to have the controller automatically find and display a file from the filesystem (matched by the name of the requested method) without having to manually set up a function for each existing file...

Or if I'm missing your point, can you explain how to do this using View?
#4

[eluser]NemetraL[/eluser]
I can't write down a step-by-step tutorial right now.. but I just wanted to highlight the fact that the View library is independant from the controller that calls it. Its a separate object and you can make it behave the way you want.

The current view library is quite simple and its only interesting feature, to my view, is its independancy. Based on that existing code, you could develop yours.

Let's say your controller calls:
Code:
$this->view->load();

.. and your load() method looks for the matching path/to/template and loads it.
From the library, you can access the called controller and method using the $ci instance (n gives you the nth segment of the requested URL):
Code:
$ci =& get_instance();
$ci->uri->segment(n);

.. based on that, you look for the correct file.

Happy coding!
;-)
#5

[eluser]Andrew Vit[/eluser]
Thanks, I didn't realize this works independent of the controller. I'll have a closer look at that.

--AV
#6

[eluser]esra[/eluser]
[quote author="Andrew Vit" date="1188261648"]Thanks, I didn't realize this works independent of the controller. I'll have a closer look at that.

--AV[/quote]

Note that there is a view method in the Loader library for the CI framework, but no independent View library. Coolfactor's solution adds a View library to the framework. It's important to understand this distinction.

Actually, Coolfactor's proposed View library works in conjunction with the view method in the Loader library and CI's Cache library. You still need to call your views within your controller. The Loader loads a file or files stored in the views directory. Other than the view directory search path, the view method in the Loader library makes no distinction between the views loaded by your controllers, blocks (independent and possibly dynamic view fragments), and master views (basically PHP templates). Coolfactor's View library takes advantage of the manner in which views are loaded by the Loader library (Loader loads a file from views/ without really knowing anything about what it loads; the file is assumed to be a view file).

Unlike a template engine, Coolfactor's master views are PHP and do not have to be parsed a second time like a template parser, so you will not experience a significant reduction in speed (although there is some speed reduction). However, it does support caching.

With the View library, you can load complex collections of views within a master view and those views can be nested in any manner you choose within the master template. You can do the same using stand CI approaches, but the View library methods simplify the process and make view loading easier to understand.
#7

[eluser]NemetraL[/eluser]
I think there's a limit to the nested views in the View library though..

When on the one hand you create parts and on the other hand you assign variables, the order in which you create parts is important: let's say you have a master view, containing part 1 which contains part 1.1, and let's say there are PHP variables in all these views that you assign using the set() method; then if you create part 1 before part 1.1 it's not gonna work since one variable within part 1 is the part 1.1 but as the part 1.1 hasn't been mentionned yet, part 1 is created without part 1.1.

At least that's what I noticed.
And I think it's pretty hard to avoid this loading order problem without a template engine, which I don't support by-the-way.




Theme © iAndrew 2016 - Forum software by © MyBB