PHP code example of yahyamallak / masar

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

    

yahyamallak / masar example snippets




use Masar\Exceptions\NotFoundException;
use Masar\Http\Request;
use Masar\Routing\Router;

$router = new Router();

// Define routes
$router->get('/', function() {
    return "Welcome to the homepage!";
});

$router->get('/about', function() {
    return "About us page";
});

$router->post('/submit', function() {
    return "Form submitted";
});

$router->put('/users/{id}/change', function($id) {
    return "User " . $id . " has been edited.";
});

$router->patch('/users/{id}/edit', function($id) {
    return "edit name of user : " . $id;
});

$router->delete('/users/{id}/delete', function($id) {
    return "delete user " . $id;
});

// Create a request object
$request = new Request();

// Dispatch the router
try {
    $router->dispatch($request);
} catch (NotFoundException $e) {
    echo $e->getMessage();
}



$router->get('/user/{id}', function($id) {
    return "User profile for user with ID: " . $id;
});

$router->get('/post/{slug}', function($slug) {
    return "Displaying post: " . $slug;
});



$router->get('/user/{id}', function($id) {

    return "User profile for user with ID: " . $id;

})->where(["id" => ":number"]);



$router->get('/users', function() {

    return "All users.";

})->name("users");



use Masar\Routing\Route;

Route::get("users");



$config = [
    "controllers" => "App\Controllers",
    "middlewares" => "App\Middlewares"
];



$router = new Router($config);



$router->get('/profile/{id}', [UserController::class, "index"]);



$router->patch('/posts/{id}/edit', "PostController@edit");



$router->get("/admin", function() {
    
    return "Admin dahsboard.";

})->middleware("auth");



$router->middleware("auth")->group(function() use($router) {
    
    $router->get("/", [HomeController::class, "index"]);

    $router->get("/about", [AboutController::class, "index"]);

});



$router->middleware(["auth", "role"])->group(function() use($router) {
    
    $router->get("/", [HomeController::class, "index"]);

    $router->get("/profile", [UserController::class, "profile"]);

});



$router->prefix("admin")->group(function() use($router) {
    
    $router->get("/", [AdminController::class, "index"]);

    $router->get("/settings", [AdminController::class, "settings"]);

});