PHP code example of danielspeir / rust

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/ */

    

danielspeir / rust example snippets


// Get router instance
$router = Rust::getRouter();

// Build 'blog' Parent Route
$router->route('blog', function(){

  $this->response(function(){
    return $this->renderView('blog');
  });
  
});

// Build 'blog/new' Child Route
$router->route('blog/new', function(){

  $this->get(function(){
    return $this->renderView('blog-add');
  });

  $this->post(function(){
    // On successful insert to database:
    return $this->redirect('blog/' . $newBlogId);
  });
  
});

// Build 'blog/:id' Dynamic Child Route
$router->route('blog/:id', function(){

  $this->response(function($id){
    return $this->renderView('blog-single', compact('id'));
  });
  
});

// Serve routes
$router->serve();


  $router->route('blog', 'Show all blogs');

  $router->route('blog', function(){
	// Route Scope
  });

$router->route('/', function(){
  // Route Scope
});

$router->index(function(){
  // Route Scope
});

	$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');

  });

  $router->route('blog', function(){
    $this->controller('ctrlFile', 'ctrlClass');
  });

  $router->group('blog', function(){
    
    // Route: 'blog' (Option 1)
    $this->route('/', 'String Return');

	// Route: 'blog' (Option 2)
	$this->index('String Return');

	// Route: 'blog/new'
	$this->route('new', 'String Return');

    // Dynamic Route: 'blog/:id'
    $this->route(':id', 'String Return');
    
  });

	$router->route('blog/:id/:action', function(){
	
	  $this->response(function($id, $action){
	    return $action . ' Blog Number ' . $id;
	  });
	  
	}); 

$router->castParams([
  'id' => 'int',
  'action' => 'string'
]);

/* 
  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);
    });
  });
});

$router->route('blog/:iId', function(){
  $this->get(function($id){
    return gettype($id);
  });
});

  // Most Specific
  $router->route('blog/new', 'Add a new blog');

  // Less Specific
  $router->route('blog/:id', 'Show blog with id.');

$router->all(function(){
  // Route Scope
});

$router->route(':all', function(){
  // Route Scope
});

$router->otherwise(function(){
  // Route Scope
});

$router->route(':otherwise', function(){
  // Route Scope
});

// Store an item
$router->store()->userId = 32;

// 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->redirect('/');
  });
  
});

$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');
  });
});

array(
  'first_name' => 'daniel',
  'last_name' => 'speir'
);

<h1>
   echo $first_name . ' ' . $last_name; 

$router->route('blog/:id', function(){
  $this->get(function($id){
    // Using get_defined_vars()
    return $this->renderView('blog', get_defined_vars());

    // Using compact()
    return $this->renderView('blog', compact('id'));
  });
});

if ($router->isAjax()) { 
  // render json data
}

$this->liveRoute();

$this->liveRouteParent();

if ($this->isRoute('blog/:id')){
  // do something
}

if ($this->isRouteParent('blog')){
  // do something
}

if ($this->isParent()){
  // do something
}

// Return full array
$router->getQueryString();

// Return one value
$router->getQueryString('myVar');

$router->setRequestMethod('post');

$router->getRequestMethod();

if ($this->isDelete()){
  // do something
}

$router->config([
  'dev' => true,
  'controller_directory' => 'controllers',
  'view_directory' => 'views',
  'death_message' => 'Page not found!'
]);

$router->config([
  'controller_directory' => 'controllers';
]);

$router->route('blog', function(){
  $this->get(function(){
	// Requires 'controllers/myCtrl.php'
    $this->controller('myCtrl');
  });
});

class blogController {

  public function index(){
    return $this->rust->json($data);
  };

}

$router->config([
  'view_directory' => 'views';
]);

$router->route('blog', function(){
  $this->get(function(){
	  // Requires 'views/blog.php'
	  return $this->renderView('blog');
  });
});

$router->route('blog', function(){
  $this->get('Get Blogs');
});

$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
  }

}

class blog {

	// innards

}

$router->route('blog', function(){
  $this->controller('blogController', 'blog');
});

$router->route('blog', function(){
  $this->controller('app/controllers/blogController.php');
});

class blogController {

  public function index(){
    return $this->rust->json($data);
  };

}

$router = Rust::getRouter();

// Build your routes

$router->serve();


// Get router instance
$router = Rust::getRouter();

// Define any configuration
$router->config([
  'view_directory' => 'views'
]);

// Serve from the 'routes' directory.
$router->serveFromDirectory('routes');

// Route: 'blog' (Option 1)
$this->route('/', 'String Return');

// Route: 'blog' (Option 2)
$this->index('String Return');

// Route: 'blog/new'
$this->route('new', 'String Return');

// Dynamic Route: 'blog/:id'
$this->route(':id', 'String Return');

$this->get('Return this string to the Index Route.');

$router->route('/', function(){
  $this->get('Return this string to the Index Route.');
});