27/04
2018
Wallogit.com
2017 © Pedro Peláez
Simple PHP Router
Simple Routing and View System for PHP Projects, (*1)
composer require cooky/url-router
root your_project_dir
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
location ~ \.ico {
add_header Content-Type application/x-icon;
}
location / {
rewrite ^(.*)$ /index.php/$1 last;
}
RewriteEngine On RewriteRule ^(.*)$ index.php/$1 [QSA,L]
vendor/cooky/url-router/config/configs/, (*2)
/* main index.php */
require_once 'vendor/autoload.php';
/* */
use Routy\Route;
/* basic get */
Route::get('/', function (){
echo '
home page
';
});
/* controller */
Route::get('/home', 'Home@Index');
/* with parameter */
Route::get('user/profile/{id}', 'User@Profile');
Route::post('test/post/{id}', function ($id){
echo $id;
});
Route::complete();
/* layout.php */
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/default.css">
<!-- Custom Css -->
@yield(css)
<title>routy basic templating</title>
</head>
<body>
<!-- NAV -->
<nav>NAV</nav>
<!-- Dynamic Content -->
@yield(content)
<!-- FOOTER -->
<footer>FOOTER</footer>
<script src="node_modules/vue/dist/vue.js"></script>
<!-- Dynamic Script -->
@yield(script)
</body>
</html>
/* home.php */
@section(css)
<link rel="stylesheet" href="css/home.css">
@stop
@section(content)
{{message}}
<!-- pass data from controller -->
<div><?php echo $test; ?></div>
@stop
@section(script)
@stop
<?php use View\Loader;
/* Home controller - every controller must be extends with Loader for view */
class Home extends Loader
{
public function index(){
$data['test'] = 'hello kitty';
$this->view('main', $data);
}
}