Great question! Let me explain what will happen when you host this website:
Yes, it will work on hosting because we're using direct MySQLi connections through our custom DatabaseHelper class instead of CI4's database system. This approach is actually more reliable for hosting because:
The issue we encountered was likely due to one of these reasons:
By creating our own DatabaseHelper, we bypassed these issues completely.
When you move to hosting, you'll need to:
Update the database connection details in the DatabaseHelper class:
self::$connection = new \mysqli('your-host', 'your-username', 'your-password', 'your-database');Make sure your hosting supports:
Security: Our current implementation is simple but could be improved for security:
// Add this to DatabaseHelper to prevent SQL injection
public static function escape($value) {
return self::getConnection()->real_escape_string($value);
}For a production site, you might want to:
But for a basic shopping website, what we've built will work fine on hosting!
Perfect! Let's continue with the project.
Let's test what we just built:
http://localhost/tesaja/public/adminhttp://localhost/tesaja/public/admin/productshttp://localhost/tesaja/public/admin/addProductUpdate app/Config/Routes.php:
<?php
namespace Config;
$routes = Services::routes();
if (file_exists(SYSTEMPATH . 'Config/Routes.php')) {
require SYSTEMPATH . 'Config/Routes.php';
}
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);
// Basic routes
$routes->get('/', 'Home::index');
// Shop routes
$routes->get('shop', 'Shop::index');
$routes->get('shop/product/(:num)', 'Shop::product/$1');
// Admin routes
$routes->get('admin', 'Admin::index');
$routes->get('admin/products', 'Admin::products');
$routes->match(['get', 'post'], 'admin/addProduct', 'Admin::addProduct');
if (file_exists(APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php')) {
require APPPATH . 'Config/' . ENVIRONMENT . '/Routes.php';
}Create app/Controllers/Home.php:
<?php
namespace App\Controllers;
class Home extends BaseController
{
public function index()
{
$html = "<!DOCTYPE html>
<html>
<head>
<title>Welcome - Shopping Website</title>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
header { background: #333; color: white; padding: 1rem; }
nav { display: flex; justify-content: space-between; align-items: center; }
.container { max-width: 1200px; margin: 0 auto; padding: 1rem; }
.hero { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 4rem 0; text-align: center; }
.btn { background: #4CAF50; color: white; padding: 15px 30px; text-decoration: none; border-radius: 5px; display: inline-block; font-size: 18px; }
.btn:hover { background: #45a049; }
.features { display: flex; gap: 2rem; margin: 3rem 0; }
.feature { flex: 1; text-align: center; padding: 2rem; border: 1px solid #ddd; border-radius: 5px; }
footer { background: #333; color: white; padding: 1rem; text-align: center; margin-top: 2rem; }
</style>
</head>
<body>
<header>
<div class='container'>
<nav>
<h1>Shopping Website</h1>
<div>
<a href='" . base_url('shop') . "' style='color: white; margin-right: 10px;'>Shop</a>
<a href='" . base_url('admin') . "' style='color: white;'>Admin</a>
</div>
</nav>
</div>
</header>
<div class='hero'>
<div class='container'>
<h2>Welcome to Our Shopping Website</h2>
<p>Discover amazing products at great prices!</p>
<a href='" . base_url('shop') . "' class='btn'>Start Shopping</a>
</div>
</div>
<div class='container'>
<div class='features'>
<div class='feature'>
<h3>Quality Products</h3>
<p>We offer only the best quality products from trusted brands.</p>
</div>
<div class='feature'>
<h3>Fast Delivery</h3>
<p>Quick and reliable delivery to your doorstep.</p>
</div>
<div class='feature'>
<h3>Great Prices</h3>
<p>Competitive prices and amazing deals every day.</p>
</div>
</div>
</div>
<footer>
<div class='container'>
<p>© " . date('Y') . " Shopping Website. All rights reserved.</p>
</div>
</footer>
</body>
</html>";
return $html;
}
}Now test these URLs:
http://localhost/tesaja/public/http://localhost/tesaja/public/shophttp://localhost/tesaja/public/adminLet me know what you want to work on next: