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 = 'http://xxx.com/images/';
        			$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:'http://xxx.com/images/nophoto-200x200.jpg');	
    						$tmp["image"]=$path;
    						$tmp["brand"] = $task["middle_name"];
    						$tmp["model"] = $task["last_name"];
    						$tmp["insurance_policy_number"] = $task["honorific_suffix"];
    						$tmp["createdAt"] = $task["date_added"];
    						$tmp["assignedTo"]=$task["display_name"];
    						$tmp["issuedAt"]=$task["issuedDate"].",".$task["issuedTime"];
    						$tmp["returnedAt"]=(($task["returnedDate"]=='0000-00-00')?'notReturned':$task["returnedDate"].",".$task["returnedTime"]);
    						$tmp["availability"]=(($task["returnedDate"]!='0000-00-00')?true:false);
    						array_push($response["cars"], $tmp);
    					}
    					
    					echoRespnse(200, $response);
    				} else {
    					$response["error"] = true;
    					$response["message"] = "We couldn't find any similerities";
    					echoRespnse(404, $response);
    				}
    			}
    			else{
    				$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):

<?php

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;
})->add('authenticate');

// Run app
$app->run();

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