Welcome Guest, Not a member yet? Register   Sign In
The MVC design pattern, how CI really works, etc
#1

[eluser]AzizLight[/eluser]
Hello everybody,

I am working on a project that is very poorly coded, and my job is to enchance it. The project as a whole is pretty complex, mainly because of the database design. For instance, the database is full of "Stocked Procedures", which to be honest, as a PHP/MySQL Guru Wanabe (which means I'm pretty much a newcomer with little experience Tongue) I have no clue what it is. The database administrator, with whom I work, explained to me that they are basically SQL scripts called by other SQL query (that's my understanding, I might be totally wrong). In short, apparently I need to use PDO to be able to make the whole app tick; I will also resuse most of the code, I will just clean it up as much as I can and rewrite only if I have to.

My solution to this whole thing was to create a whole MVC environement from scratch and rewrite (or reroute) the whole thing. Now that is a bit challenging for somebody like me (with little php experience, I know I'm repeting myself).

I found a couple tutorials online on how to create his own MVC based php app. Here are the links I have found:

- PHP Frameworks? Just roll your own! – PART 1
- Model View Controller MVC
- Build your own MVC Framework - Step One: Getting Started
- Write your own PHP MVC Framework (Part 1)

Now as a CodeIgniter fanatic, I looked at the codeigniter code to compare it, and I was kind of surprised. All the tutorials above use a centralized index.php file using a .htaccess file with some code similar to the following (except one I think - the one uses a .htaccess file too though):

Code:
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]

My instinct tells me that codeigniter also uses a centralized index.php file, but I didn't find a .htaccess file inside the folder structure. That would either mean that I'm wrong or that there some magic happening, probably in the Router library.

I will obviously look more deeply into the code, but I was hoping somebody could briefly explain to me how the whole thing works in CI, and how the url is rewritten if there is no .htaccess...

Also, I would L-O-V-E to use CodeIgniter instead of creating a customized MVC app from scratch, but I'm also aware that I should use the correct tool for the correct job. So do you think I should use CodeIgniter for what I want to do? Or should I start from scratch?
#2

[eluser]alboyd[/eluser]
Stocked Procedures? Do you mean Stored Procedures?

I would also say yes you are probably right - the route library takes care of a lot of that. Hence everything hangs off index.php unless you remove that by using HTACCESS.

Without knowing what you need to achieve it's hard to say what the best solution is. However I'm with you - if it can be done with CI I'll do it with CI. (if it can't be done with CI I just won't do it) Smile

Good luck
#3

[eluser]bretticus[/eluser]
You don't have to have an .htaccess file. The rewrite configuration could very well be in the apache config file (httpd.conf or a standalone configuration file for that virtual domain.) On the other hand, what makes you think that the poorly coded project uses the MVC pattern at all? Does it have the "fancy/SEO/REST" URL scheme?

Codeigniter does use an index.php to coordinate all of the subsequent files that are to be loaded and executed. It is diagrammed in the manual even:


Again, an .htaccess file can be optional if you put the rewrite config in the apache config file. But there MUST be a rewrite config SOMEWHERE for URL rewriting to take place. Otherwise, you'll get URLs with a index.php/method/controller scheme.

They are Stored Procedures (not "Stocked".) They are not uncommon for use with a website. It's just a way to put commonly used queries on the database server instead of initiating them from the client (web server) each time. Also, you can do more programming within these procedures (loop, if structures, etc.) AS for having to use PDO, well, if you rewrite this as a CI application, you technically, don't have to use PDO to access these queries. You can call procedures using a standard query. See Stored procedures under the MySQL Manual.

If you have to use PDO by mandate, there are tutorials out there for modifying CI to work with PDO.
#4

[eluser]BrianDHall[/eluser]
CodeIgniter uses a "front-controller", which is the centralized index.php file. It all starts there, and the htaccess file reroutes everything through it. It is located in the top level of installation. It defines some constants, sets a few things, and then continues on with loading the framework and getting everything going.

The main difference in the htaccess file is with Routes - CI doesn't like the query string style, thus its a bit different in implementation.

For using the right tool for the job, the question would be what is it you want from an MVC app that CI doesn't do? What are the major features of your app you seek?

From my 30-second analysis of your description of your problem, the difficulties are database integration. Designing a whole MVC system from scratch seems like a total waste of time, when you could use CI and get coding the stuff that actually matters...well, right now.

n0xie posted about something like this before, where what they did was design a system where they just wrapped the whole old site in CI. If a function didn't exist in CI, then they automatically passed the request through right over to the old application. Thus they could take their time in recreating/changing anything that needed changing, yet they always had a full working site and 1 unified codebase to track.

I think you would best spend your time reading up on stored procedures and PDO and don't worry about custom coding an MVC model.
#5

[eluser]AzizLight[/eluser]
Thanks for your answers. Sorry about the misnamed "Stocked" Procedures. I tried to translate the term from french, was very far Smile

The poorly written web app does not use MVC. My goal is to rewrite the app so that it does. Hence my question.

Now I want to say something: BrianDHall, sir, you are convincing. Everytime you answer one of my post not only do I get the response I was waiting for, but you also manage to make me change my mind completely!

Now I don't want to code my own MVC now! lol

On a more serious note, my first impression is that the Design of Database is rather complicated, and that really the source of the problem. Also another problem is that I don't know a thing about stored procedures, I will start documenting myself right away.

bretticus: the link you provided is ... well it's not a link, it's just http:// lol but thanks anyway, I managed to find a couple good link using the big G.

About the purpose of the app itself, it's not THAT complicated. It's basically a FrontEnd GUI for the database. The employees use it to add, edit, remove and basically manage music albums. What's more complicated is all the types of albums, types of tracks, etc but in my opinion that's somewhat related to the database design (which is, once again, complicated => source of the problem).

Now if I understood well, there is a way to use/call stored procedures using "simple" mysql queries. Then if this is the case then using CI won't is not even questionable, I HAVE TO USE IT! Smile

Anyway, no more useless talking, I will go document myself and see how things go.

Thanks again for all the clear answers Smile
#6

[eluser]n0xie[/eluser]
[quote author="AzizLight" date="1256673316"]
Now if I understood well, there is a way to use/call stored procedures using "simple" mysql queries. Then if this is the case then using CI won't is not even questionable, I HAVE TO USE IT! Smile
[/quote]

Code:
$this->db->query('CALL proc_name(args)');
#7

[eluser]AzizLight[/eluser]
[quote author="n0xie" date="1256673549"][quote author="AzizLight" date="1256673316"]
Now if I understood well, there is a way to use/call stored procedures using "simple" mysql queries. Then if this is the case then using CI won't is not even questionable, I HAVE TO USE IT! Smile
[/quote]

Code:
$this->db->query('CALL proc_name(args)');
[/quote]

I see, I understood well Smile That's very interesting, thanks a lot.
#8

[eluser]jedd[/eluser]
I'm a bit of a hater of Stored Procedures .. the motivation for shifting code into SP's is often misguided, or at least misunderstood by the original advocate.

Do you have the original design documentation handy?

It might be feasible to start shifting the functionality of the stored procedures back out into your model(s) or perhaps a library. SP's, especially on MySQL, make backups and recovery a tad more challenging, if only because most *nix guys forget that they have to be backed up separately to the database proper. You can imagine the Joy that this often brings after recovering from a major outage.




Theme © iAndrew 2016 - Forum software by © MyBB