PHP code example of wpmvc / routing

1. Go to this page and download the library: Download wpmvc/routing 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/ */

    

wpmvc / routing example snippets


	/**
     * Define an object or a value in the container.
     *
     * @param string $name Entry name
     * @param mixed $value Value, define objects
     */
    public function set(string $name, $value) {}
	

	
    /**
     * Returns an entry of the container by its name.
     *
     * @template T
     * @param string|class-string<T> $name Entry name or a class name.
     *
     * @return mixed|T
     */
    public function get($name) {}
	

	 /**
     * Call the given function using the given parameters.
     *
     * Missing parameters will be resolved from the container.
     *
     * @param callable $callable   Function to call.
	 * 
     * @return mixed Result of the function.
     */
    public function call($callable) {}
	


    

    namespace MyPlugin\Providers;

    use MyPlugin\Container;
    use WpMVC\Routing\Providers\RouteServiceProvider as WpMVCRouteServiceProvider;

    class RouteServiceProvider extends WpMVCRouteServiceProvider {

        public function boot() {

            /**
             * Set Di Container
             */
            parent::$container = new Container;

            /**
             * OR you use PHP-Container 
             * Uses https://php-di.org/doc/getting-started.html
             */
            // parent::$container = new DI\Container();


            /**
             * Set 

	add_action('init', function() {
		$route_service_provider = new \MyPlugin\Providers\RouteServiceProvider;
		$route_service_provider->boot();
	});
	



use WpMVC\Routing\Route;
use WpMVC\Routing\Response;
use WP_REST_Request;

defined('ABSPATH') || exit;

Route::get('user', function(WP_REST_Request $wp_rest_request) {
	return Response::send(['ID' => 1, 'name' => 'john']);
});

Route::get('user', [UserController::class, 'index']);

// Required id
Route::get('users/{id}', [UserController::class, 'index']);

// Optional id
Route::get('users/{id?}', [UserController::class, 'index']);

Route::group('admin', function() {

    Route::get('/',  [UserController::class, 'index']);

    Route::group('user', function() {
        Route::get('/', [UserController::class, 'index']);
        Route::post('/', [UserController::class, 'store']);
        Route::get('/{id}', [UserController::class, 'show']);
        Route::patch('/{id}', [UserController::class, 'update']);
        Route::delete('/{id}', [UserController::class, 'delete']);
    } );
} );

Route::resource('user', UserController::class);

use WpMVC\Routing\Ajax;
use WP_REST_Request;

Ajax::get('user', function(WP_REST_Request $wp_rest_request) {
	return Response::send(['ID' => 1, 'name' => 'john']);
});

$site_id   = get_current_blog_id();
$namespace = 'myplugin';

$rest_route_path = get_rest_url($site_id, $namespace);

$user_rest_route = $rest_route_path . '/user';

$ajax_route_path = get_site_url($site_id) . '/' . $namespace;

$user_ajax_route = $ajax_route_path . '/user';



namespace MyPlugin\App\Http\Middleware;

use WpMVC\Routing\Contracts\Middleware;
use WP_REST_Request;

class EnsureIsUserAdmin implements Middleware
{
    /**
    * Handle an incoming request.
    *
    * @param  WP_REST_Request  $wp_rest_request
    * @return bool
    */
    public function handle( WP_REST_Request $wp_rest_request ): bool
    {
        return current_user_can( 'manage_options' );
    }
}

parent::$properties = [
	...
	'middleware' => [
		'admin' => \MyPlugin\App\Http\Middleware\EnsureIsUserAdmin::class
	]
];

Route::get('/admin',  [AdminController::class, 'index'], ['admin']);

    routes:
        rest:
            api.php
            v1.php
            v2.php
        ajax:
           api.php
           v1.php