1. Go to this page and download the library: Download morningtrain/wp-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/ */
morningtrain / wp-route example snippets
use Morningtrain\WP\Route\Route;
Route::setup(__DIR__ . '/routes');
// /routes/myroute.php
use \Morningtrain\WP\Facades\Route;
// Set up a route on the /myroute URL and call MyrouteController::myRoute as callback
Route::get('/myroute','MyrouteController::myRoute');
// /Controllers/MyrouteController.php
class MyrouteController extends \Morningtrain\WP\Route\Abstracts\AbstractController{
public static function myRoute(){
// Validate request & send http status code
// Fetch data
// Render view
}
}
// /routes/kittens.php
use \Morningtrain\WP\Facades\Route;
// Set up a route on the /kitten/1 URL and call KittenController::kittenById as callback
Route::get('/kitten/{kitten_id}','KittenController::kittenById');
// /Controllers/KittenController.php
class KittenController extends \Morningtrain\WP\Route\Abstracts\AbstractController{
public static function kittenById(\Symfony\Component\HttpFoundation\Request $request){
// Validate request & send http status code
// Fetch data
$kitten_id = $request->query->get('kitten_id');
// Render view
}
}
// /routes/kittens.php
use \Morningtrain\WP\Facades\Route;
// Set up a route on the /kitten/1 URL and call KittenController::kitten as callback
Route::get('/kitten/{kitten_id}','KittenController::kitten')->name('kitten');
// /Controllers/KittenController.php
class KittenController extends \Morningtrain\WP\Route\Abstracts\AbstractController{
public static function kitten(\Symfony\Component\HttpFoundation\Request $request){
// Validate request & send http status code
// Fetch data
$kitten_id = $request->query->get('kitten_id');
// Render view
}
}
// In some template or hook you can now get and check for the named route
// Get Url for Kittens Route
$url = Route::route('kittens',['kitten_id' => 1]); // Would return /kitten/1
// Check if currently on kitten route
$bool = Route::current()->getName() === 'kitten';
use \Morningtrain\WP\Facades\Route;
Route::prefix('my-prefix')->group(function(){
Route::get('foo',FooController::class); // url will be /my-prefix/foo
Route::get('bar',BarController::class); // url will be /my-prefix/bar
})
use \Morningtrain\WP\Facades\Route;
// Users must now be logged in to view these two routes
Route::middleware('auth')->group(function(){
Route::get('foo',FooController::class);
Route::get('bar',BarController::class);
})
use \Morningtrain\WP\Facades\Route;
Route::prefix('my-prefix')
->middleware('auth')
->group(function(){
Route::get('foo',FooController::class);
Route::get('bar',BarController::class);
})
use Symfony\Component\HttpFoundation\Request;
// Controller
class FooController extends \Morningtrain\WP\Route\Abstracts\AbstractController{
public function __invoke(Request $request){
// Do something here
$post = $request->query->get('post');
}
}
// Middleware
function(Request $request, $next){
if($request->query->has('post')){
// Do something here
$post = $request->query->get('post');
}
return $next($request);
}
use Morningtrain\WP\Facades\Route;
use \Symfony\Component\HttpFoundation\Request;
Route::middleware([function(Request $request, $next){
if(!\is_user_logged_in()){
// Returns a Response object
return \Morningtrain\WP\Route\Classes\Response::with404();
}
// Continues the middleware pipeline
return $next($request);
}])
->group(function(){
Route::get('my-account',MyAccountController::class);
});
use Morningtrain\WP\Facades\Rest;
Rest::get('products', [ProductRestController::class, 'getAllProducts'])->public(); // Will register an endpoint like /wp-json/mtwp/v1/products that accepts GET requests
use Morningtrain\WP\Facades\Rest;
Rest::namespace('foo/v1')->group(function(){
Rest::get('foo',FooController::class); // /wp-json/foo/v1/foo
});