Manage empty strings

i am just playing with slim framework and its amazing!.here i am developing this search function in my API my question is if by any chance i send an empty string the result is 404 i tried to manage this inside function,but it fails.As i am new to this can anyone suggest the best approach?

        $app->get('/search/:str', 'authenticate', function($searchq) {
                    global $user_id;
        			$car_manager_upload_url = '';
        			$car_manager_upload_dir = '/home/public_html/images/';
                    $response = array();
                    $db = new DbHandler();

                // fetch task
                if($searchq!=NULL || $search!=""){
    				$result = $db->getVehicles($searchq);

    				if ($result != NULL) {
    					$response["error"] = false;
    					$response["cars"] = array();
    					// looping through result and preparing cars array
    					while ($task = $result->fetch_assoc()) {
    						$tmp = array();
    						$tmp["carid"] = $task["vehicleId"];
    						$tmp["reg_number"] = $task["first_name"];
    						$tmp["slug"] = $task["slug"];
    						$carOptions = unserialize($task["options"]);
    						$carName = basename($carOptions["image"]["meta"]["original"]["url"]);
    						$path = (file_exists($car_manager_upload_dir.$tmp["slug"].'/'.$carName)?$car_manager_upload_url.$tmp["slug"].'/'.$carName:'');	
    						$tmp["brand"] = $task["middle_name"];
    						$tmp["model"] = $task["last_name"];
    						$tmp["insurance_policy_number"] = $task["honorific_suffix"];
    						$tmp["createdAt"] = $task["date_added"];
    						array_push($response["cars"], $tmp);
    					echoRespnse(200, $response);
    				} else {
    					$response["error"] = true;
    					$response["message"] = "We couldn't find any similerities";
    					echoRespnse(404, $response);
    				$response["error"] = true;
    				$response["message"] = "Empty String";
    				echoRespnse(404, $response);

Hello rkvisit,

My answer is a bit late, I hope it is still still useful to you.

The code you provided does not look like Slim Framework version 3. Are you perhaps using Slim Framework version 2? The placeholders have a different notation and the callback function signature is different as well.

I have created a minimal example for use with Slim Framework version 3 ( I have left out the authentication middleware):


require_once __DIR__ . '/vendor/autoload.php';

$app = new \Slim\App([
    'settings' => [
        'displayErrorDetails' => true

$app->get('/search/{str:[^/]*}', function($request, $response, $args) {
    $searchString = $args['str'];

    // TODO implement search for $searchString
    $response->write('Searched for "' . htmlspecialchars($searchString) . '"');

    return $response;

// Run app

I have specified [/^]* as the pattern for the placeholder str, because the default pattern (which is [^/]+) matches one or more characters instead of zero or more characters. This way the route will also match /search/, the case of the empty search string.

1 Like