John Davidson

php - Every URL is showing me "/" (index) with Heroku

0 comments
Message:


I just finished my first MVC project with PHP and MySQL. I created a router that allows me to when I visit an URL, call a function that render the expected file.


I'm doing it with lampp's PHP and MySQL, but not using Apache, otherwise I'm using the php server (php -S localhost:3000 -t public/). There, my project works perfectly well, and the routing is working like normal.


Well, I deployed it on Heroku but the problem is that my routing just doesn't work; every time I try to go (clicking a button or just typing it on the searchbar) to, for example, /about-us it just render "/" (index) again.


Here's my index.php (here I have all of my urls and call the functions that render everything)


<?php 

require_once __DIR__ . '/../includes/app.php';

use MVC\Router;
use Controllers\PropiedadController;
use Controllers\VendedorController;
use Controllers\PaginasController;
use Controllers\BlogController;
use Controllers\AuthController;

$router = new Router();


$router->get('/admin', [PropiedadController::class, 'index']);

$router->get('/propiedades/crear', [PropiedadController::class, 'crear']);
$router->post('/propiedades/crear', [PropiedadController::class, 'crear']);
$router->get('/propiedades/actualizar', [PropiedadController::class, 'actualizar']);
$router->post('/propiedades/actualizar', [PropiedadController::class, 'actualizar']);
$router->post('/propiedades/eliminar', [PropiedadController::class, 'eliminar']);

$router->get('/vendedores/crear', [VendedorController::class, 'crear']);
$router->post('/vendedores/crear', [VendedorController::class, 'crear']);
$router->get('/vendedores/actualizar', [VendedorController::class, 'actualizar']);
$router->post('/vendedores/actualizar', [VendedorController::class, 'actualizar']);
$router->post('/vendedores/eliminar', [VendedorController::class, 'eliminar']);

$router->get('/blog/crear', [BlogController::class, 'crear']);
$router->post('/blog/crear', [BlogController::class, 'crear']);
$router->get('/blog/actualizar', [BlogController::class, 'actualizar']);
$router->post('/blog/actualizar', [BlogController::class, 'actualizar']);
$router->post('/blog/eliminaradminpropiedades', [BlogController::class, 'eliminar']);

$router->get('/', [PaginasController::class, 'index']);
$router->get('/nosotros', [PaginasController::class, 'nosotros']);
$router->get('/anuncios', [PaginasController::class, 'propiedades']);
$router->get('/anuncio', [PaginasController::class, 'propiedad']);
$router->get('/blog', [PaginasController::class, 'blog']);
$router->get('/entrada', [PaginasController::class, 'entrada']);
$router->get('/contacto', [PaginasController::class, 'contacto']);
$router->post('/contacto', [PaginasController::class, 'contacto']);

//Login

$router->get('/login', [AuthController::class, 'login']);
$router->post('/login', [AuthController::class, 'login']);
$router->get('/logout', [AuthController::class, 'logout']);

$router->comprobarRutas();

Here's my router.php


<?php 

namespace MVC;

class Router {

public $rutasGET = [];
public $rutasPOST = [];

public function get($url, $fn) {
$this->rutasGET[$url] = $fn;
}

public function post($url, $fn) {
$this->rutasPOST[$url] = $fn;
}

public function comprobarRutas() {

session_start();
$auth = $_SESSION['login'] ?? null;

//Arreglo de rutas protegidas
$rutas_protegidas = ['/admin', '/propiedades/crear', '/propiedades/actualizar', '/propiedades/eliminar', '/vendedores/crear', '/vendedores/actualizar', '/vendedores/eliminar/', '/blog/crear', '/blog/actualizar', '/blog/eliminar'];


$urlActual = $_SERVER['PATH_INFO'] ?? '/';
$metodo = $_SERVER['REQUEST_METHOD'];

if($metodo === 'GET') {
$fn = $this->rutasGET[$urlActual] ?? NULL;
} else {
$fn = $this->rutasPOST[$urlActual] ?? NULL;
}

if(in_array($urlActual, $rutas_protegidas) && !$auth) {
header('Location: /');
}

if($fn) {
call_user_func($fn, $this);
} else {
echoPre('ERROR 404');
}


}

public function render($view, $datos = []) {

foreach($datos as $key => $value) {
$$key = $value;
}

ob_start();
include __DIR__ . "/view/$view.php";
$contenido = ob_get_clean();
include __DIR__ . "/view/layout.php";
}

}

Here's my .htaccess


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Here's my Procfile


web: vendor/bin/heroku-php-apache2 public/


My Answer:
It looks like the issue might be related to the way Heroku handles routing. Since you are using the built-in PHP server for local development, the routing works fine. However, when deployed to Heroku, the routing is not working as expected.

One thing you can try is to update your .htaccess file to include the following lines:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]


This will ensure that all requests are redirected to your index.php file, where your router logic is defined.

Additionally, make sure that your Procfile is correctly pointing to the public directory where your index.php file is located:


web: vendor/bin/heroku-php-apache2 public/


If after making these changes the issue persists, you may need to debug further to see if there are any other configuration settings or server settings on Heroku that are causing the routing issue.

Rate this post

5 of 5 based on 9331 votes

Comments




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