Crag
Crag

Reputation: 107

php include - alternative with better url

i want to build a website with different views, but a stable header and footer - no problem so far. But i dont like the kind of urls i got at the moment with the php GET method. My site at the moment works like this (what istn working properly):

$_page = $_GET['p'];
if ($_page == "city-sitemap"){ include "views/city-sitemap-view.php"; }

if ($_page == "place"){ include "views/place-view.php"; }


else { include "views/index-view.php";}

this isnt a very sweet solution but i dont know a other for now. I tried to use a mvc framework but failed dramatically. So everytime i add a link i use for example this "index.php?p=place" - not very nice. The including of the views isn very smart as well? is there a better way? I would like to use something like the rewriteEngine that the new url is like a folder. Can you help me to find a better solution? Thanks a lot

Upvotes: 1

Views: 357

Answers (3)

ooo
ooo

Reputation: 1627

Page including

For the page inclusion, you can use a simple array to dynamically allocate your page to a specific name. As so:

$pages = array('city-sitemap'=>'views/city-sitemap-view.php',
    'place'=>'views/place-view.php',
)

if(array_key_exists($_GET['p'], $pages){
    include $pages[$_GET['p']];
}else{
    include 'views/error.php';
}

This array should be added in a general configuration file. With this configuration if you want to display your city-sitemap-view.php view, you will have to write this url: http://www.domain.com/index.php?p=city-sitemap

Url rewriting

It is possible to rewrite an URL with a .htaccess file. Here is an example of code you would can to write in your .htaccess file.

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

An url that looks like this:

http://www.domain.com/index.php?p=city-sitemap

will be converted to this one:

http://www.domain.com/city-sitemap

Upvotes: 2

Steve
Steve

Reputation: 20469

As per my comment, i really think you should consider the framework i linked, (it is very logical) or any other micro framework, but if you really wish to do this yourself then you can handle your includes like so:

<?php  //index.php
$requested_page=isset($_GET['p'])?$_GET['p']:'home';

//maybe have this included from pages.php for organization
$pages=array(
    'home'=>'home_content.php',
    'about'=>'about_content.php',
    'contact'=>'contact_content.php'
);

include "views/header.php";
if(array_key_exists($requested_page, $pages)){
    include "views/".$pages[$requested_page];
}else{
    header('HTTP/1.0 404 Not Found');
    include "views/error404.php";
}
include "views/footer.php";

This keeps your pages in a single array, and protects against arbitrary inclusion vulnerabilities.

For nicers urls, see The other users .htaccess rewrite rules

Upvotes: 0

Zak
Zak

Reputation: 25215

There is something called the front controller that you should look into. You can write a very simple one yourself. It works in conjunction with URL rewriting. If for example your url looks like this: /mypage.html

the url rewriter will reroute the request to index.php .

Index.php will then look at the URL and do somehting like this:

1) break the page out into string, ignoring .html extention

 preg_match("\/(.*?)\.html", $_REQUEST['URI'], $matches);
 $pageClass = $matches[1];
 //$pageClass = "mypage"

2) look for and load a class named "Mypage.php"

 $controller = new $pageClass();

3) call the run method on the page class, passing it all the request parameters

 $request = new Request($_REQUEST);
 $controller->run($request);

you can then do all the page specific stuff inside your controller class that is specific to the page.

At each simple step along the way, you will find you want to do more and more things like authentication, filtering, tracking, etc. You will get end up developing a front controller that is specific to your needs, as well as a base Controller class that does a bunch of standard stuff that all your controllers have in common.

Upvotes: 0

Related Questions