PHP code example of nicollassilva / minasrouter

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

    

nicollassilva / minasrouter example snippets



    MinasRouter\Router\Route;

// The second argument is optional. It separates the Controller and Method from the string
// Example: "Controller@method"
Route::start("http://yourdomain.com", "@");

Route::get("/", function() {
    // ...
});

// ... all routes here

// You will put all your routes before this function
Route::execute();

Route::get('/users', [\App\Controllers\User::class, 'index']);
Route::post('/users', [\App\Controllers\User::class, 'store']);
Route::put('/users/{id}', [\App\Controllers\User::class, 'update']);
Route::patch('/users/{id}', [\App\Controllers\User::class, 'update']);
Route::delete('/users/{id}', [\App\Controllers\User::class, 'delete']);

// The router allows you to register routes that respond to any HTTP verb:
Route::any('/', function() {
    // ...
});

// Sometimes you may need to register a route that responds to multiple HTTP verbs:
Route::match(["GET", "POST"], "/", function() {
    // ...
});

Route::get("/users/create", function() {
    // ...
})->name("user.create");

Route::get("/users/2", function() {
    // ...
})->as("user.show");

Route::get("/", function() {
    // ...
})->name("web.index");

Route::get("/user/{id}", function($id) {
    echo $id;
})->name("user.show");

Route::get("/post/{id?}", function($id) {
    if(!$id) {
        // ...
    }
    
    // ...
})->name("post.show");

Route::get("/user/{id}", [\App\Controllers\UserController::class, "show"])
    ->name("user.show")
    ->where(["id" => "[0-9]+"]);

// whereParam is alias of where method
Route::get("/profile/{slug}", [\App\Controllers\UserController::class, "profile"])
    ->name("user.profile")
    ->whereParam("id", "[0-9]+");

Route::get("/book/{id}", [\App\Controllers\BookController::class, "show"])
    ->name("book.show")
    ->whereNumber("id");

Route::globalMiddlewares([
    'isLogged' => \App\Middlewares\isLogged::class,
    'isAdmin' => \App\Middlewares\isAdmin::class
]);

// ... all routes

Route::get("/musics", function() {
    // ...
})->middleware("isLogged");

Route::get("/musics", function() {
    // ...
})->middleware(["isLogged"]);

Route::get("/musics", function() {
    // ...
})->middleware("first, second");

Route::get("/movies", function() {
    // ...
})->middleware(["first", "second"]);

use App\Middlewares\VerifyCsrfToken;

Route::get("/series", function() {
    // ...
})->middleware(VerifyCsrfToken::class);

Route::get("/series", function() {
    // ...
})->middleware(App\Middlewares\VerifyCsrfToken::class);

Route::middleware(['auth', 'api'])->group(function() {
    Route::get('/', function() {
        // All middlewares will works in this route
    });
    
    Route::get('/no-api', function() {
        // Only the auth middleware works here
    })->withoutMiddleware('api');
});

Route::name("admin.")->group(function() {
    Route::get("/", function() {
        // admin.index
    })->name("index");
});

Route::prefix("admin/")->group(function() {
    Route::get("/index", function() {
        // http://localhost/admin/index
    })->name("index");
});

Route::namespace("App\Controllers")->group(function() {
    Route::get("/user/{id}", ["User", "show"])->name("show");
    // \App\Controllers\User
});

Route::middleware(\App\Middlewares\isLogged::class)->group(function() {
    Route::get("/user/{id}", ["User", "show"])->name("show");
});

Route::namespace("App\Controllers\Admin")
    ->middleware(["isLogged", "isAdmin"])
    ->name("admin.")
    ->prefix("admin")
    ->group(function() {
    // ...
});

Route::namespace("isLogged")->group(function() {
    Route::get("/posts", function() {
        // ...
    })->middleware("isAdmin");
    
    // ...
});

Route::name("admin.")->group(function() {
    Route::get("/posts", function() {
        // name: app.posts
    })->name("app.posts", true);
});

// Returns 302 status code by default.
Route::redirect("/here", "/there");

Route::redirect("/here", "/there", 301);

// permanentRedirect always returns 301
Route::permanentRedirect("/here", "/there");

// You can return an existing route
Route::redirect("/index", "web.index");

Route::fallback(function() {
    echo 'Route error!';
    // ...
});

use \MinasRouter\Http\Request;

Route::get("/", function(Request $request)) {
   // ... 
});

Route::get("/user/{id}", function($id, Request $request)) {
   // ... 
});

Route::get("/posts/{slug?}", function($slug, Request $request)) {
   // ... 
});

Route::get("/book/{slug}", function($slug, Request $request) {
    // Retrieving all dynamic parameters
    print_r($request->getParams());
});


$uri = $request->path();

$url = $request->url();

$urlWithQueryString = $request->fullUrl();

$value = $request->header("Header-Name");

$value = $request->header("Header-Name", "default");

if ($request->hasHeader("Header-Name")) {
    // ...
}

$token = $request->bearerToken();

$ipAddress = $request->ip();

$id = $request->query("id");

$developer = $request->query("developer", "Nicollas");

$query = $request->query();

// http://localhost/?foo=bar

$foo = $request->foo;

// <input type="text" name="title" value="MinasRouter">

$title = $request->title;

$data = $request->all();

// all, except csrf_token, page
$data = $request->all("csrf_token, page");

$httpMethod = $request->getMethod();

if ($request->isMethod('POST')) {
    // ...
}
nginx
location / {
  if ($script_filename !~ "-f"){
    rewrite ^(.*)$ /index.php?route=/$1 break;
  }
}