Help with Simple PHP Boostrap

Heya,

I can’t seem to figure out how frameworks do this -

site.com/Controller/Function/value

if everything is redirected to an index.php via .htaccess file we can say…

if (first url part == something) load class

then the second part I don’t get, would you just be like:

if (second url == something)
$loadedClass->FunctionInURL(thirdURLValue)

Yes in most cases, the use of default could do the best but in some cases the users should be notified what happened when s/he (site visitor) tried to access an non existed page. In some cases we must throw/give some proper information to the user like maybe for some time the page was removed and will come back.

So actually it depends on the requirement.

Why do you make an exception for the home page? Why not define a default controller and a default function for each controller?
If no controller is defined use the default controller, if no function is defined use the default function.

And one more thing, I hope you have prevented the direct access of the controller file though the class and its methods are called outside of the controller file itself but it is better to define a constant in index.php and check in controller page whether it is defined or not and if not, just throw some message.

I am slowly figuring it out heres my sloppy code so far :slight_smile:

/**
* Handle the Request
*/
if (isset($_GET['url'])) 
{
    /**
    * Create array of the URL
    * http://workspace/Controller/Function/Value
    */
    $url = explode('/', $_GET['url']);
    
    if (file_exists("controllers/$url[0].php"))
    {
        require_once("controllers/$url[0].php");
        $object = new $url[0];
        
        if (!empty($url[1]))
        {
            $object->{$url[1]};
        }
    }
    else
        echo "The file: '$url[0].php' does not exist in your controllers.";
}
else
echo 'home page';


One more check is needed if method exists in the loaded class as far as I know (what I have checked in my case):


...................
if (!empty($url[1]) && method_exists($object, $url[1]))
        {
            $object->{$url[1]};
        }
...................

Oh thats a good one, I kept thinking of function_exists (or whatever the equivalent is), that will work nice :wink:

It’s also a good idea to prefix the functions that can be called by the visitors with a prefix like “action” or something. For example “actionLogin” would then be accessible on [noparse]http://www.example.com/controllerName/login[/noparse]
That way the users can not call each and every method of your controller, only those prefixed with “action”.