开发者

help understand mvc php

ok yesterday i open a thread about when to use mvc,

today I'm about to learn how MVC frameworks work, examining some examples like CI, CAKE, etc

  1. on the .htaccess i found this

    RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/?url=$1 [QSA,L]

ok so when we type http://localhost/mymvc/something1/something2/something3/somethingetc

we got an $_GET['url'] =string 'something1/something2/something3/somethingetc' (length=45)

2.so I suggest the something1 will be the class, something2 must be the function and something3 im not quite sure, how does exectly framework loads the class ?, functions ?

class Blog extends Controller {

    function index()
    {
        echo 'Hello World!';
    }
    function stack()
    {
        echo 'Hello Stack!';
    }      
}

3.ok so , i found that every framework first loads the config files, then loads a front-controller this is a front-controller ( on ci ) looks, i assume that they do like this ?

  • extends the class ?
  • they get the name of the class ? then require_once controller.nameclass.php
  • then they somehow search for functions ? ( how do they do that ? )
  • next they look for the default function ( function index ) then loads it ?
  • if there is a client call the url /Blog/stack it loads just the Stack function, i dont >know how that work either .
  • if we put $this->loadview('something') so i assume that they call the function loadview ( that is inside the Controller class and require them by the name, like require_once somethin开发者_JAVA百科g.php

Maybe there is a part two of this :|,

Thanks a lot.

Adam Ramadhan


  1. The framework that built this .htaccess file, uses PHP to decompose its elements (possibly slower than Apache would do it with more complex declarations in .htaccess)
  2. Yes, it determines the class (Controller) and the method to call (Action), and passes the rest of the parameters to that method. It uses call_user_func or something similar to call the functionality after the framework's core functionality has executed.
  3. A function presence can be checked also. PHP has some basic Reflection implementation, which allows you to see what the class/object is built of.

Controller classes in many cases inherit a base class, for this functionality.

This doesn't really give you much. You'd have better luck with the frameworks if you learn how to work with it, not how it works.

It might help if you take a course of pure MVC (not linked to any framework), or if you're in a hurry, try this.


Here's a very simplified example: http://r.je/mvc-in-php.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜