Map web requests to PHP handlers and validate route assumptions.

Before starting: PHP functions, HTTP and Composer.

Practice project: Extend the examples into a small Laravel study demo. Explain the setup, one normal case and one failure or boundary case.

Start here: concepts and guided practice

  1. Laravel: respond to a URL

    A route maps a request to application behavior. This GET route returns JSON. Laravel is a PHP framework, so PHP is the language you write inside the route handler.

    Existing compatible Laravel application. Add snippets to routes/web.php; start its local development server.

    use Illuminate\Support\Facades\Route;
    
    Route::get("/study-status", function () {
        return response()->json(["ready" => true]);
    });
    Check the output
    GET /study-status returns JSON containing {"ready":true}.

    Change ready to false and inspect the response.

  2. Laravel: constrain a route parameter

    A route parameter reads a value from the path. A numeric constraint prevents this route matching non-numeric IDs. It does not prove that the learner exists or that the caller may access the learner.

    Existing compatible Laravel application. Add snippets to routes/web.php; start its local development server.

    use Illuminate\Support\Facades\Route;
    
    Route::get("/learners/{id}", function (string $id) {
        return response()->json(["id" => $id]);
    })->whereNumber("id");
    Check the output
    /learners/7 returns id "7"; /learners/abc does not match this route.

    Explain which separate checks are needed for existence and authorization.

Open the Laravel learning path on DevStudyHub · Play coding games on this blog

logoblog

No comments:

Post a Comment

Please do not enter any spam link in the comment box.