PHP code example of proteins / route
1. Go to this page and download the library: Download proteins/route library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
proteins / route example snippets
use Proteins\Route;
Route::on('/hello',function(){
echo 'Hello, Friend!';
});
Route::on('/hello')
->via('get','post')
->with(function(){
echo 'Hello, Friend!';
});
Route::map('/entity(/:id)/?',[
'get' => function($id=null){
// READ: fetch the $id element or all if $id === null
},
'post' => function($id=null){
// CREATE: build a new element
},
'put' => function($id=null){
// UPDATE: modify $id element's properties
},
'delete' => function($id=null){
// DELETE: delete $id element
},
])
Route::any('/hello',function(){
echo 'Hello, World!';
});
Route::on('/hello')
->via('*')
->with(function(){
echo 'Hello, World!';
});
Route::on('/element(/:id)/?',function($id=null){
if (null === $id){
$result = get_all_elements();
} else {
$result = get_element_by_id($id);
}
print_r($result);
});
Route::on('/element(/:id)/?',function($id=null){
if (null === $id){
$result = get_all_elements();
} else {
$result = get_element_by_id($id);
}
print_r($result);
})
->rules([ 'id' => '\d+' ]);
Route::group('/admin',function(){
Route::on('/',function(){
echo "Admin Index";
});
Route::on('/login')
->via('get','post')
->with(function(){
// Handle login
});
Route::on('/logout',function(){
// handle logout
});
Route::group('/dashboard',function(){
Route::on('/',function(){
// Dashboard
});
Route::on('/details',function(){
// Dashboard Details
});
});
});
Route::group("/book/:id", function($id){
$book = new Book($id);
Route::on("/", function() use ($book){
return $book;
});
Route::on("/:field", function($field) use ($book){
return $book->$field;
});
});
Route::on('/',
"[TEST]"
)
->before(function(){
echo "(B1)";
})
->before(function(){
echo "(B2)";
})
->after(function(){
echo "(A1)";
})
->after(function(){
echo "(A2)";
});
Route::group('/private',function(){
Route::on('/', ... );
Route::on('/dashboard', ... );
Route::on('/profile', ... );
Route::on('/settings', ... );
})->before(function(){
if ( ! user_authorized() ) {
Response::error(403,"Forbidden");
return false;
}
});
Route::on('/hello',function(){
echo 'Hello, Friend!';
});
// Run the route dispatcher.
Route::dispatch();
Route::dispatch($URL=null, $method=null, $return_route=false)
Route::dispatch('/my/forced/uri','OPTIONS');
$matched_route = Route::dispatch(null,null,true);
Event::on(404,function(){
Response::html( View::from('errors/404') );
});
Route::on('/',function(){
return View::from('index');
});
Route::on('/', View::from('index') );
Route::on('/', 'Not a callable string' );
Route::on('/',(object)[
'alpha' => 123,
'beta' => [1,2,3]
]);
Route::on('/', function() {
return View::from('index');
})->push([
'style' => '/assets/css/main.css',
'script' => [
'/assets/js/vendors.js',
'/assets/js/main.js',
],
]);
Route::on('/user/:id', function ($id) {
return "USER[$id]";
})->tag('user');
$user_route = Route::tagged('user');
echo $user_route->getURL();
echo Route::URL('user');
echo Route::URL('user',[
'id' => 123,
]);
Route::onEvent('start',function($route, $args, $method){
echo "Called route.\n";
});
/user
/user/123