I'm using the Slim Framework to create some web services, to make a search by keyword in my web application. My problem is that when I search by keyword that have a forward slash (/), the slim interprets the slash as a route and the response is a 404 not found
.
For example if my keyword is: "One" an HTTP GET request for /events/One
will invoke the associated callback function, passing "One" as the callback's argument. But if my keyword is: "One/Two", an HTTP GET request for /events/One/Two
will invoke the associated callback function, because the Slim expects a route like this events/:parm1/parm2
.
How can I solve this?
$app->get('/eventos/genero/:genero', 'findByGenero');
function findByGenero($genero) {
$sql = "SELECT id, id_organizador, imagem, titulo, latitude, longitude, endDate, startDate, local, rua FROM eventos_sapo WHERE genero LIKE :genero ORDER BY titulo";
try {
$db = getConn();
$stmt = $db->prepare($sql);
$genero = "%".$genero."%";
$stmt->bindParam("genero", $genero);
$stmt->execute();
$eventos = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($eventos);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
If url encoding the parameter does'nt work then you could use a query paraemter rather than a route parameter:
$app->get('/eventos/genero', 'findByGenero');
function findByGenero() {
$app = \Slim\Slim::getInstance();
$genero = $app->request->get('q');
if ($genero) {
$sql = "SELECT id, id_organizador, imagem, titulo, latitude, longitude, endDate, startDate, local, rua FROM eventos_sapo WHERE genero LIKE :genero ORDER BY titulo";
try {
$db = getConn();
$stmt = $db->prepare($sql);
$genero = "%".$genero."%";
$stmt->bindParam("genero", $genero);
$stmt->execute();
$eventos = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($eventos);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
}
The URL is now: /eventos/genero?q=2134
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.