Skip to content

Instantly share code, notes, and snippets.

@noook
Created May 3, 2018 12:08
Show Gist options
  • Select an option

  • Save noook/906d32d597a478eb7db43a480f89a91b to your computer and use it in GitHub Desktop.

Select an option

Save noook/906d32d597a478eb7db43a480f89a91b to your computer and use it in GitHub Desktop.
New Router
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/assets(/.*|)$ [NC]
RewriteRule .* index.php
# /etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
127.0.0.1 website.url # Change URL here
255.255.255.255 broadcasthost
::1 localhost
# /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf
#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
DocumentRoot /usr/local/var/www
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/path/to/folder" # Change path to website
ServerName website.url #Change URL
</VirtualHost>
<?php
require_once('autoload.php');
require_once('vendor/autoload.php');
use Symfony\Component\Yaml\Yaml;
use Routing\Router;
$config = Yaml::parseFile('config/config.yml');
$params = Yaml::parseFile('config/parameters.yml');
session_start();
$loader = new Twig_Loader_Filesystem('views');
$twig = new Twig_Environment($loader, array(
//'cache' => 'cache/twig',
'cache' => false,
));
$request = substr($_SERVER['REQUEST_URI'], 1);
$request = explode('?', $request)[0];
if ($request == '') {
$request = 'home';
}
$router = new Router();
$response = $router->execute($request);
echo $response;
<?php
namespace Routing;
use Cool\DBManager;
class Router
{
public function execute($request)
{
global $config;
if (isset($config['routes'][$request]))
{
$data = explode(':', $config['routes'][$request]);
$controller_name = "Controller\\".$data[0].'Controller';
$method_name = $data[1].'Action';
$controller = new $controller_name;
$response = call_user_func([$controller, $method_name]);
return $response;
}
else
{
header("HTTP/1.0 404 Not Found");
http_response_code(404);
$data = explode(':', $config['routes']['error404']);
$controller_name = "Controller\\".'MainController';
$method_name = 'error404Action';
$controller = new $controller_name;
$response = call_user_func([$controller, $method_name]);
return $response;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment