John Davidson

url - Using Klein PHP to access routing parameters


I am trying to offer an an endpoint to the API I am building that accepts routing parameters.Something like:

to allow param as string to act as the parameter for the call to the endpoint. So all I really need is the ability to know what the string after the slash is.

I understand to do this I need something to handle routing for me, and Klein php seems to be a popular light weight routing ... framework.

I am sure this is very simple but the documentation isn't really clear on it to me - here is what I am trying

$klein = new \Klein\Klein();
$results = $klein->respond(function () {
return 'Everything';

print_r($results );

But this gives

    Klein\Route Object
] => Closure Object

] => *
] =>
] =>
] =>

Something obvious I'm doing wrong here? Thanks!

EDIT: per @ArSeN comment I have updated to :

$klein = new \Klein\Klein();

$klein->respond(function () {
return 'All the things';


But, nothing happens, how can I actually access and use the string that comes after / in the same way I would a get param like $_GET['param']


My Answer:
>param('param', function($param) {
echo "The parameter is: " . $param;


Rate this post

3 of 5 based on 9375 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us