CI 4 What are Closure Routes?

Share this Article

Closure routes are those routes in which we don’t bind controller and it’s method for url function. Instead we process route function by using closure callback function.

$route->get("<route>", function(){ ... });

When we define route closures, it means we are going to call request function in route file itself.

//...

// route with static message
$routes->get("contact-us", function () {

    echo "<h1>Welcome to Online Web Tutor</h1>";
});

// route with view file
$routes->get("about-us", function () {

    return view("about-us"); // /app/Views/about-us.php
});

// route with parameter
$routes->get("services/(:num)", function ($id) {

    echo "ID: ".$id;
});

//...
  • /contact-us, /about-us etc route is handled by callback function.

Inside all above route, we didn’t map any controller and method. You can use any method type.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5