puelo
puelo

Reputation: 6047

Rewrite URL in a somewhat MVC Framework?

I am not quite sure if this is done by rewriting the URL, but I did not know how to title it accordingly.

Here is my problem. I am currently working on a website project where I am using a abstraction of the MVC Framework (it is mainly for learning the Framework). This is how my folder-structure looks like:

/controller/
 |--indexcontroller.php

/myaccount/
 |--/controller/
    |--indexcontroller.php
 |--index.php

/globals/
 |--framework.php

/templates/

/options/
 |--settings.php
 |--config-www.php.inc

So currently I am using autoloader to load the classes which are needed. The index.php in the myaccount folder inherits the Framework class which should handle the class loading:

$urlparts = explode("/", $_SERVER['REQUEST_URI']);
    $urlparts2 = explode("?", $urlparts[2]);
    $class = str_replace(".php", "", $urlparts2[0]);
    
    if ($class == "") {
        
        $class = "index";
    }

    $letters_first = substr($class, 0, 1);
    $letters_last = substr($class, 1, strlen($class));
    
    $class = strtoupper($letters_first).$letters_last."Controller";

    if (class_exists($class)) {

        $object = new $class();
        
    } else {
       
        echo "Problem: class $class does not exist.";
    }

The problem i have at the moment is, that I can only use "http://www.url.com/myaccount/" which is loading the indexcontroller.php in the controller folder from myaccount (which is fine). But I want also be able to use "http://www.url.com/myaccount/profile", where then instead "profilecontroller.php" in the controller folder from myaccount should be called.

How can I do this? URL rewriting? Or am I doing it totally wrong?

Upvotes: 0

Views: 175

Answers (1)

Robin Drost
Robin Drost

Reputation: 507

This might be a bit overwhelming at first but if you get it, it will make you life a lot easier.

1 ) Take a look at an existing framework like symfony, codeigniter, etc to see how they get the routing done.

2 ) Try to reuse these "components" (like routing form symfony) through composer so you won't have to do it yourself.

This way:

  • You won't have to write it all yourself.
  • You are using the hot stuff, like composer :)
  • You still learn by looking at the implementation of other frameworks

I hope this helps a bit :).

Upvotes: 1

Related Questions