11 Replies - 527 Views - Last Post: 29 August 2016 - 01:47 PM Rate Topic: -----

#1 Lazy Vulpes  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 91
  • Joined: 02-May 12

Front controller and constructing html response.

Posted 29 August 2016 - 04:37 AM

I'd like my index.php to be my front controller and have it construct a html response. This is what I have so far:

.htaccess
    
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]



index.php
<?php
$requestURL = $_SERVER['REQUEST_URI'];
switch($requestURL){
	case "/root/": include ("content/home.php");
	break;
	case "/root/about": include ("content/about.php");
	break;
	default: include ("content/404.php");
}



I've been told by someone here that including html inside my php is bad practice. How should I go about doing this?

Is This A Good Question/Topic? 0
  • +

Replies To: Front controller and constructing html response.

#2 Atli  Icon User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Front controller and constructing html response.

Posted 29 August 2016 - 04:52 AM

It's indeed bad to mix HTML and PHP code together, in the same file, but that doesn't necessarily mean that include() -ing the HTML into a PHP file is bad. They key concern here is keeping the logic and the output (the HTML) separated.

Look into templating systems like Twig and Smarty.

You can also do the same thing they do with pure PHP, but you need to be careful not to go overboard with the PHP code inside the templates. Anything beyond simple conditions and loops belongs in the application code, not the template.
Was This Post Helpful? 0
  • +
  • -

#3 Lazy Vulpes  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 91
  • Joined: 02-May 12

Re: Front controller and constructing html response.

Posted 29 August 2016 - 06:54 AM

I'll probably be using pure php, I want to keep my system as simple as possible without too much of an overhead.

Thanks a lot for your help, I'll look into the template system you mentioned.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3786
  • View blog
  • Posts: 13,715
  • Joined: 08-August 08

Re: Front controller and constructing html response.

Posted 29 August 2016 - 08:34 AM

Arrays can help simplify code.
$routes = array("/root/"=>"content/home.php", "/root/about"=>"content/about.php");
if(array_key_exists($requestURL,$routes)) {
	include $routes[$requestURL];
} else {
	include ("content/404.php");
}

This way changes to the routes only require a simple change to the array, not the code.
Was This Post Helpful? 0
  • +
  • -

#5 Lazy Vulpes  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 91
  • Joined: 02-May 12

Re: Front controller and constructing html response.

Posted 29 August 2016 - 08:46 AM

View PostCTphpnwb, on 29 August 2016 - 08:34 AM, said:

Arrays can help simplify code.
$routes = array("/root/"=>"content/home.php", "/root/about"=>"content/about.php");
if(array_key_exists($requestURL,$routes)) {
	include $routes[$requestURL];
} else {
	include ("content/404.php");
}

This way changes to the routes only require a simple change to the array, not the code.


The code might be more simple, but the abstraction is greater. This is not a bad thing if you're comfortable with the level of abstraction, but I'm not. :(

Thanks a lot for your input, it's much appreciate, but I'm too stupid to do it your way :P (for now at least)
I'll definitely keep your suggestion in mind though. :)
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3786
  • View blog
  • Posts: 13,715
  • Joined: 08-August 08

Re: Front controller and constructing html response.

Posted 29 August 2016 - 08:52 AM

All it says is: "If the requested url is in the array of available urls, use that, otherwise use the 404." Try it. If you don't like it, you can take it out, but learning by doing is a good way to make things stick!
Was This Post Helpful? 0
  • +
  • -

#7 Lazy Vulpes  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 91
  • Joined: 02-May 12

Re: Front controller and constructing html response.

Posted 29 August 2016 - 11:16 AM

View PostCTphpnwb, on 29 August 2016 - 08:52 AM, said:

All it says is: "If the requested url is in the array of available urls, use that, otherwise use the 404." Try it. If you don't like it, you can take it out, but learning by doing is a good way to make things stick!


I understand it perfectly well, it's just not very intuitive to me. Objectively, your approach is better, subjectively, it's another story.

I have to explain my code at an exam at some point, it's important that it's my code, and that I know it like the back of my own hand.

I'll see if I can rewrite it to suit my background in programming better, I've only been writing php for about 6 months.
Was This Post Helpful? 0
  • +
  • -

#8 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2517
  • View blog
  • Posts: 10,093
  • Joined: 03-December 12

Re: Front controller and constructing html response.

Posted 29 August 2016 - 11:42 AM

Since you will be retyping a lot of the same, '/root/' + whatever, it would be a wiser approach to split the string, use that last value in the uri and just load that. But, this approach requires you to maintain the displayed naming conventions.

$uriParts = explode('/', $_SERVER['REQUEST_URI']);
$template = end($uriParts);

if( is_file("content/$template.php") ) {
    include "content/$template.php";
} else {
    include "content/404.php";
}


Was This Post Helpful? 0
  • +
  • -

#9 Lazy Vulpes  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 91
  • Joined: 02-May 12

Re: Front controller and constructing html response.

Posted 29 August 2016 - 12:55 PM

Thanks a lot for the suggestion astonecipher, it gave me a few ideas.
Here's what I've wrote so far:

<?php
class Application {
    public function __construct() {
        $uriParts = explode('/', $_SERVER['REQUEST_URI']);
        $this->request = end($uriParts);
        if($this->request!=""){
        	$this->content = "content/".end($uriParts).".php";
        }else{
        	$this->request="home";
        	$this->content = "content/home.php";
        }	
    }
}
<?php
include_once("application.class.php");
$app = new Application();
$routes = array("","home","about");
if(in_array($app->request,$routes)){
	include($app->content);
}else{
	include("content/404.php");
}



Any issues I'm not seeing?

This post has been edited by Lazy Vulpes: 29 August 2016 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#10 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2517
  • View blog
  • Posts: 10,093
  • Joined: 03-December 12

Re: Front controller and constructing html response.

Posted 29 August 2016 - 01:03 PM

I'm not seeing the value in the app class, if the logic is elsewhere.
Was This Post Helpful? 0
  • +
  • -

#11 Lazy Vulpes  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 91
  • Joined: 02-May 12

Re: Front controller and constructing html response.

Posted 29 August 2016 - 01:07 PM

View Postastonecipher, on 29 August 2016 - 01:03 PM, said:

I'm not seeing the value in the app class, if the logic is elsewhere.


I'm defining my dbh handler there, which a user class is using. I've just removed it from here so it'd be easier for you to read.

this is the full code
<?php

class Application {
    public function __construct() {
        $cfg = $this->getConfig();
        $dsn = "mysql:host={$cfg['host']};dbname={$cfg['dbname']}";
        $this->dbh = new PDO($dsn, $cfg['user'], $cfg['password']);
        $this->root=$_SERVER['DOCUMENT_ROOT'];
        $uriParts = explode('/', $_SERVER['REQUEST_URI']);
        $this->request = end($uriParts);
        if($this->request!=""){
        	$this->content = "content/".end($uriParts).".php";
        }else{
        	$this->request="home";
        	$this->content = "content/home.php";
        }
		
    }
    
    private function getConfig() {
        $config = parse_ini_file("dbconfig.ini");
        return $config;
    }
}



Edit: I think I might have misunderstood you :/

Edit2: No you're right, I've moved the logic into index.php idk why I put it in a separate class, just a habit I guess.

This post has been edited by Lazy Vulpes: 29 August 2016 - 01:18 PM

Was This Post Helpful? 0
  • +
  • -

#12 Lazy Vulpes  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 91
  • Joined: 02-May 12

Re: Front controller and constructing html response.

Posted 29 August 2016 - 01:47 PM

View Postastonecipher, on 29 August 2016 - 01:03 PM, said:

I'm not seeing the value in the app class, if the logic is elsewhere.

Does this make more sense?

<?php
class Router{

	private $uriParts;
	private $request;
	private $routes;

	public function __construct(){
		$this->uriParts = explode('/', $_SERVER['REQUEST_URI']);
		$this->request = end($this->uriParts);
		$this->routes = array("","home","about");
		if($this->request!=""){
		$this->content = "content/".end($this->uriParts).".php";
		}else{
			$this->request="home";
			$this->content = "content/home.php";
		}
	}

	public function content(){
		if(in_array($this->request,$this->routes)){
			return $this->content;
		}else{
			return "content/404.php";
		}
	}
}

This post has been edited by Lazy Vulpes: 29 August 2016 - 01:56 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1