Route with parameters get 404


#1

I’ve the code bellow, when I pass:

localhost/api/empresas

It works, but when I tried a route with parameters like:
localhost/api/empresas/1,

I got:
Page Not Found
The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.

Here’s my code:

Index.php:

<?php 
require 'vendor/autoload.php';

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

use App\Controllers\EmpresasController;

$app = new \Slim\App([
    'settings' => [
        'displayErrorDetails' => true,
    ]
]);

$container = $app->getContainer();

$container['db']= function($container){
	return new PDO('pgsql:host=localhost;port=5432;dbname=base', 'root', '123');
};

$container['EmpresasController'] = function($container) use ($app){
	return new App\controllers\EmpresasController($container);
};


$app->get('/', 'EmpresasController:index')->setName('home');

$app->get('/empresas', 'EmpresasController:listarTodasEmpresas')->setName('empresas');

$app->get('/empresas/{$id}', 'EmpresasController:empresaPorId')->setName('empresaPorId');

EmpresasController.php:

<?php 
namespace App\controllers;


class EmpresasController extends Controller 
{
    
    public function index($request, $response, $args){
		print_r($args);
	}

	public function listarTodasEmpresas($request, $response, $args){
		
		$stmt = $this->db->prepare("SELECT * FROM empresa");
		$stmt->execute();
		
		$customers = $stmt->fetchAll();

		echo json_encode($customers);
	}

	public function empresaPorId($request, $response, $args){
		
		print_r($args);

	}
}

And my .htaccess:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]


#2

Hi,

pleas try

$app->get(’/empresas/{id}’, ‘EmpresasController:empresaPorId’)->setName(‘empresaPorId’);

instead of

$app->get(’/empresas/{$id}’, ‘EmpresasController:empresaPorId’)->setName(‘empresaPorId’);


#3

Exactly this, solved, thanks.