Welcome Guest, Not a member yet? Register   Sign In
Moving away from the MVC pattern
#1

[eluser]xwero[/eluser]
I've already made the bald statement in this thread but i want to explain the thoughts behind it in this statement.

At the moment the front controller (CodeIgniter/codeiginter.php) checks if the controller is present to confirm if the controller/method url is valid.
Code:
if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT))

I've been exploring convenience programming for a while now and i'm beginning to realize controllers aren't necessary because i found out i'm only using them for redirects.

CI has a router class that gets executed before the controller but it has a second place because it only can intervene if a non controller/method route is set. For the rest of the time the router class doesn't do anything.

But what if you add all the allowed urls to the routes.php file and the front controller checks the url based on that list. This means the url you see is the url you get. Now there are exceptions because of the controller url structure which gives a lot of users headaches.
If an option to internationalize the urls is added you can create urls without the need of adding the language to the url.
Code:
http://site.com/user/me //english
http://site.com/gebruiker/me //dutch
http://site.com/utilisateur/me // french

The router would also have to take care of the redirects because that is a responsibility of the controller. To do this there validation should be added to get to the right page for the conditions.

I am of the opinion the controller is only a step to get to the true separation of data fetching/manipulating code and display code. The M and the V can stay but the C is up for debate in the pattern.
#2

[eluser]Randy Casburn[/eluser]
Hi xwero, While I'm contemplating this, what would you call this then...
Quote:At the moment the front controller (CodeIgniter/codeiginter.php)...

If you were eliminating controllers?

The Router class come closest to implementing (realizing) a Bridge pattern for CI. The Router class (or Bridge) can be forced if you change your routing rules right? So you can 'Bridge' in an 'abstract' way to affect the execution of your application. In fact, that's precisely what your example demonstrates by refining that abstraction into the reason for using the specific Bridge. This "Bridge Pattern" idea is substantiated by the fact that when used in the prescribed way, Router does, in fact validate (_validate_segments()). But it validates only in its abstract scope and not in scope of the 'concrete' implementation. This is because it is beyond the scope of responsibility of any Bridge Class to do this.

This implies then, that there MUST be some concrete API Class that implements the intent of the API. This is the case all APIs that would use similar constructs that use intermediary classes like the Router class to intervene "out of scope".

That means the CI API must rely on some Class construct as the concrete class. Call it whatever you want, but in an MVC construct...must folks call it the Controller. The Controller is the concrete class in this overly academic post.

Randy
#3

[eluser]Pascal Kriete[/eluser]
I'm with Randy on this, although not quite as i-have-a-degree-in-cs'ish (cause I don't).

Controller does not mean url. The controller is the class that coordinates the request - it gets the data from the model, and builds and returns the view. it does not have to be called with example.com/controller_name/method_name/args . I believe RoR introduced that concept, but it's not a foundation of the mvc pattern.

For example, the python frameworks - django and appengine come to mind - each have a configuration file that does this.

For django this is the url dispatcher and for appengine it's app.yaml. I prefer this pattern myself, and am a huge proponent of routes for that reason. However, the class that gets called in response to the url - and there always is one - is still the controller in the traditional sense. You cannot get rid of that class.

So there's that, now since I said I like the general pattern:
Quote:it has a second place because it only can intervene if a non controller/method route is set. For the rest of the time the router class doesn’t do anything.

I do believe this comes across the wrong way, cause I don't see how this only works if it's a non controller/method route. You can override any url, I think what you're saying is that you may potentially break a non-routed path.

Recap: I love the idea of specifying all possible routes, but it doesn't eliminate the controller.
#4

[eluser]nmweb[/eluser]
It also reminded me of Django but indeed, there is still a controller (though named differently). I thought it was a hassle at first but after a while I saw the beauty of it. Especially with generic views you can build an application in no-time.
#5

[eluser]xwero[/eluser]
As i mentioned the idea came to me after doing some sites using convenient programming. The model, validation and view load based on the url and the controller is the file that takes care of the redirects. Because the CI application flow is based on the MVC pattern the codeigniter.php file checks if the controller is present but because i'm loading the other components outside the scope of the controller the url is getting a bigger responsibility.

I think i wanted to create a shotcut when i said, the url you see is the url you get. There are situations rerouting is required to achieve hackable urls. For example the url site.com/username. But I'm not that found on having one segment urls because if you want to create other one segment urls you have no other option than to work with a controller or you would have to create a router url list with database data.

I have to agree the router shouldn't be in charge of the redirects but then the redirects should be done in the model class, but i don't know if that is such a good idea? Or maybe it is but i'm too influenced by the MVC pattern.

Another reason why controllers feel unnecessary to me is because most of the time it is model data that is at the base of the redirects. The application flow i propose would be

- request
- load model if needed based on url list of the router
- check if validation is needed, load validation file if needed
- If validation is needed a redirect can take place there
- check if a redirect is needed
- load the data for the view based on the (redirected) url
- load the view
- terminate application

If i find the time this week i will hack the router library and codeigniter.php file to get a working example of my ideas.
#6

[eluser]Randy Casburn[/eluser]
Hey -- I'm trying to have an open mind about this...and think I see where you are going...but I still have the hang ups that I just can't over come. For instance, please help me understand the "what" behind the "verbs" in these statements.

- load model
- check validation
- redirect
- check if a redirect is needed
- load the data
- load the view

-- What entity or routine or process or class takes the action represented by these verbs? To me this must be codeigniter.php right? If so, then all you've done here is eliminated "multiple controllers" not controllers in total. You've simply "limited" CodeIgniter to a single "front controller" and that's it.

Very efficient indeed.

Randy
#7

[eluser]xwero[/eluser]
It's not only in the codeigniter.php file but i will break it down at your request
[quote author="Randy Casburn" date="1217879332"]- load model[/quote]
codeigniter.php
Code:
if(file_exists(APPPATH.'/models/'.$RTR->trigger().EXT))
[quote author="Randy Casburn" date="1217879332"]- check validation[/quote]
codeigniter.php
Code:
if(isset($_POST) && count($_POST) > 0 && file_exists(APPPATH.'/models/'.$RTR->trigger().VALIDATION_SUFFIX)
[quote author="Randy Casburn" date="1217879332"]- redirect[/quote]
validation file
Code:
if( ! $this->validation->run()){ }else{ redirect(); }
[quote author="Randy Casburn" date="1217879332"]- check if a redirect is needed[/quote]
inside data fetching method of the loaded model
Code:
function trigger(){ if($something){ redirect(); } //... }

[quote author="Randy Casburn" date="1217879332"]- load the data[/quote]
data fetching method of the loaded model
Code:
function trigger(){ /*data fetching*/ $this->load->set_vars($data);  }
[quote author="Randy Casburn" date="1217879332"]- load the view[/quote]
codeigniter.php
Code:
if(file_exists(APPPATH.'/views/'.$RTR->trigger().EXT))

Mind you this is only pseudo code based on a few sites and i haven't hacked the router class yet. The redirect function in the code comes from the url helper but i think the router class should have a redirect method.

But is doesn't have to mean you have to abandon the DRY principle. I think there is a way to create a module library too like it's the case now with ME and matchbox.

With the current CI code you can only display one controller AFAIK so that does not change.

I think with the controllers, people put way too much code in the methods because they can. If you lose the controllers people will have to find other ways to keep their files maintainable and putting the validation in separate files is an example i already found very useful. Because it checks for the post global it's also possible to separate post and get form submits if you want.
Another example could be a file you load between the loading model and the view file to generate display components making it possible only having echos in the view files. Because i believe the designer should be able to change the html to his liking i'm not sure how to make this possible without him having to touch php code. Phptal is still a great example for me to give the designer that kind of power.
#8

[eluser]Randy Casburn[/eluser]
Thanks for taking the time to explain. I know you're busy.

Randy
#9

[eluser]Randy Casburn[/eluser]
xwero -- First, on the multiple controller thing. CI uses multiple controllers. codeigniter.php IS a controller and Welcome.php is a second controller. Clear as crystal to me. But that is for a different thread...Now..
----------------
I've discovered where we're disconnected on this conceptual think piece. I pulled this quote from another topic (http://ellislab.com/forums/viewreply/438101/):

[quote author="xwero" date="1217595091"]I don't know what your idea of a input controller is but i think a controller is nothing more than a physical confirmation of the url in your application. [/quote]

That's good because up until now I kind of missed that clarity. Your clarity, though, doesn't match any definition of what an MVC controller is anywhere that I've been able to find. (including the CI UG). That doesn't mean it's wrong. It just means the paradigm will be harder for you to shift. More importantly, if the CI adopts some radical ideal based upon some unproven design principle, made up as a grass roots effort, it could have a negative downstream affect. Ideas born from brilliance are the source of most forks.


[quote author="xwero" date="1217595091"]If there would an understandable way in which the router could take care of the application urls and the display conditions controllers aren't needed anymore.[/quote]

You're actually quite wrong here IMHO. I think, and please correct me where I go wrong, if we made routers do (almost) everything the controller does now, you are simply making the routers INTO controllers. This is what I was eluding to im my original post. It doesn't matter what you call them, the functionality is there. To me, it simply appears that you're playing a shell game. [/quote]

[quote author="xwero" date="1217595091"]Instead of files maybe a list of allowed urls could be used. Basically i'm beginning to wonder if the C is fattening the pattern for webapplications. [/quote]

inparo and I were just in a thread last night related to this. If you've not researched or seen how routing works in Ruby on Rails, you should look into that. RoR uses "named routes" that would provide a significant lift in the direction you're headed. It would allow this last concept of "predefined routes" while allowing CI to work as designed for the rest. So all of your routes could be predefined "named routes" (listed files in your terms), while someone else could continue to use the CI segmented routes the way they always have. I would have to defer to inparo for the particulars regarding RoR as I'm sure he has more expertise than I.

I see you, I hear you, I am completely in tune with your station. I've seen a lot of projects fork because of idealism.

Let's make "named routes" work as a step in the right direction...baby steps. inparo?

Randy
#10

[eluser]llbbl[/eluser]
Controllers provide a good way for inexperienced programmers to become familiar with using a framework as well as the MVC programming pattern in general. MVC is used in other languages as well, like Java.

I would think that the majority of programmers who use MVC+PHP don't know how to properly implement the design pattern or decide to skip one of the components to the pattern because of the scope of their project. If you don't want to use MVC (or don't think its useful), I would start a new Framework with a design pattern of your choosing, or combination thereof. Abstract Factory, builder, prototype, singleton, flyweight, proxy, observer ... etc




Theme © iAndrew 2016 - Forum software by © MyBB