use Slim\Factory\AppFactory; require __DIR__ . '/../vendor/autoload.php'; $app = AppFactory::create(); $app->get('/hello/{name}', function ($request, $response, $args) { $name = $args['name']; $response->getBody()->write("Hello, $name"); return $response; }); $app->run();
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Matcher\UrlMatcher; use Symfony\Component\Routing\Exception\ResourceNotFoundException; $route = new Route('/hello/{name}', ['_controller' => function ($name) { return new Response('Hello, '.$name); }]); $routes = new RouteCollection(); $routes->add('hello', $route); $matcher = new UrlMatcher($routes, new RequestContext()); $request = Request::createFromGlobals(); try { $parameters = $matcher->matchRequest($request); $response = call_user_func_array($parameters['_controller'], $parameters); } catch (ResourceNotFoundException $e) { $response = new Response('Not found', 404); } catch (Exception $e) { $response = new Response('An error occurred', 500); } $response->send();This code sets up a router using the Symfony Routing package and defines a route for the `/hello/{name}` URL. When a request is received, the router tries to match it to the defined routes and finds the `hello` route. The code then calls the controller specified in the route and passes the `name` parameter to it. Finally, the controller returns a response object with the greeting message, which is sent back to the client. In both examples, the package libraries used are Slim Framework and Symfony Routing, respectively. However, there are many other libraries and frameworks that can be used for PHP URL backend development depending on the specific requirements of the application.