Question

I am tired of making script based on pages and all. I do not want to do stuffs old ways. I want to learn OOP based on url. I know how to use .htacces for url masking and rewrite rule. But the thing is that when I forwarded all queries to a PHP page, I had to use switch case statement to include files. Like if query is p=profile then I need to include profile.php file either manually or by function. But I do not want do this type of things. I want to learn professional PHP so I can create webapps like wordpress and elgg and all. I've tried finding online tutorials about it but it didn't work for me.

I hope that at least one person will help me correct way.

Was it helpful?

Solution 2

Generally the parameters in the urls are used to call a respective class/function. Let say we have these urls:

  • example.com/index.php?controller=foo
  • example.com/index.php?controller=foo&function=edit
  • example.com/index.php?controller=bar

At index.php you could start playing with includes like below:

$controller = $_GET["controller"];
include("controllers/{$controller}");
$theClass = new $controller();

Some web applications work with a "default funcion" that is triggered when a function is not specified in the url. For example, an index function:

$function = $_GET["function"];
if (empty($function))
    $function = "index";  // the default function to be called

$theClass->$function();

The Foo class can looks like this:

class Foo{

    function index(){
        echo "hello index";
    }


    function edit(){
        echo "editing foo";
    }

}
  • For the url example.com/index.php?controller=foo the output will be hello index
  • For the url example.com/index.php?controller=foo&function=edit the output will be editting foo

Note: You can use $_SERVER['QUERY_STRING'] instead $_GET to give urls more "friendly".

OTHER TIPS

There are many ways to do this. The gist of it is though, use convention in how you name your controllers and their methods. Use URL rewriting to map all requests to a single request dispatcher, then logic in that class to load the appropriate resource (as you mentioned). But don't use a giant switch, instead do something like:

  1. Requested URL: http://my.host.com/blog/hello-world
  2. Rewrite URL to something like: dispatcher.php?q=blog/hello-world
  3. In dispatcher, parse q and consider: a) Does class controllers/Blog.php exist? If so instantiate b) Does class Blog of a helloWorld method? If so, call it

This is a brain dead example, but maybe it will get you started.

My advice: don't reinvent the wheel. Use a quality framework like Laravel or Yii or _ (insert favorite framework here). This will save you immeasurable amounts of time. But if you want to or have to write it from scratch, consider downloading such a framework and learning by example.

How about :

<?php

if(!isset($_GET['page'])){$_GET['page'] = 'index';}
$whiteList = array('index', 'page1', 'page2');

$controller = in_array($_GET['page'], $whiteList) ? $_GET['page'] : 'index';

$controller = new $controller();
$controller::indexAction();

?>

Edit : Added the call to a controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top