1. Go to this page and download the library: Download danielspeir/rust 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/ */
$router->route('blog', function(){
// Return a view on Get
$this->get(function(){
// This is the Response Method Scope
return $this->returnView('blogs');
});
// Manually echo a string on Post
$this->post(function(){
// Option 1:
echo "Posting a blog!";
// Option 2:
return "Posting a blog!";
});
// Let Rust echo the string on Delete
$this->delete('No deleting is allowed');
});
// The Parent Route
$router->route('blog', function(){
// Renders only before this Parent Route
$this->before('String Return');
// Renders before Parent and Child Route(s)
$this->beforeAll('String Return');
// Renders only before Child Route(s)
$this->beforeChildren('String Return');
});
// The Child Route
$router->route('blog/:id', function(){
// Renders only before this Child Route,
// but after the beforeAll and beforeChildren
// from the Parent.
$this->before('String Return');
});
/*
Global Level
Specificity Level: Least Specific.
Will cast ALL ':id' Route parameters defined
in your application as 'int', unless overwritten
by a more specific castParams.
*/
$router->castParams(['id' => 'int']);
$router->group('blog', function(){
/*
Namespace Level
Specificity Level: Moderately Specific.
Will overwrite any Global Level castParams
and will cast all ':id' Route parameters
used within this Namespace as 'int'.
*/
$this->castParams(['id' => 'int']);
$this->route(':id', function(){
/*
Route Level
Specificity Level: Most Specific.
Will overwrite any Global and Namespace
Level castParams and will cast the ':id'
Route Parameter as an 'int' only for this
Route.
*/
$this->castParams(['id' => 'int']);
$this->get(function($id){
return gettype($id);
});
});
});
// Retrieve an item
echo $router->store('userId');
// Retrieve an item
echo $router->store()->userId;
$router->cleanStore();
$router->route('blog/:id', function(){
$this->get(function($id){
$blogRecord = // fetch a blog record by $id
return $this->json($blogRecordObject);
});
});
$router->route('blog', function(){
$this->get(function(){
return $this->renderView('blog');
// Or, if it were an HTML file
return $this->renderView('blog.html');
});
});
$router->config([
'view_directory' => 'views';
]);
$router->route('blog', function(){
$this->get(function(){
// renders 'views/blog.php'
return $this->renderView('blog');
// Or, if view_directory is not set:
return $this->renderView('views/blog');
});
});
$router->route('blog/:id', function(){
$this->controller('blogController');
/*
String will now refer to a controller method,
and any arguments will be automatically passed
to that method.
*/
$this->get('index');
// A Closure argument will not be affected by this
$this->post(function(){
// Will render normally
});
});
class blogController {
public function index($id){
// renders on Get request to 'blog' route
}
}