CodeIgniter Forums

Full Version: Best structure for my "Online Shop"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey,

I want to create a simple online shop with CodeIgniter 4,
I created a "base" Controller called "Pages.php" and I created a "renderProducts" Method for rendering the "Product Page" and I created a "fallback" Method for every page that doesnt have a special method ("renderOther")

Code:
<?php

namespace App\Controllers;

use CodeIgniter\Controller;


class Pages extends Controller{

    public function index()
    {

        $this->renderOther("home");

    }

    public function renderProducts($id){

        helper("server");
        helper("currency");


        if(!ServerEntryExists($id)){

            throw new \CodeIgniter\Exceptions\PageNotFoundException();

        }else{




            $this->server_model = new \Server_model();
            $data["theme"] = "sms_default/bootstrap.css";
            $data["servers"] = $this->server_model->get_servers();

            $this->product_model = new \Product_model();
            $data['products'] = $this->product_model->get_products_for_server($id);

            $data["serverstring"] = convertServerIDToString($id);

            echo view('templates/header', $data);
            echo view('pages/products', $data);
            echo view('templates/footer', $data);

        }

    }


    public function renderOther($page = "home")
    {


        helper("server");
        helper("currency");

            # Render everything else
        if(!is_file(APPPATH."/Views/pages/".$page.".php")){
            throw new \CodeIgniter\Exceptions\PageNotFoundException($page);
        }


        $this->server_model = new \Server_model();

        $data['title'] = ucfirst($page); // Capitalize the first letter
        $data["theme"] = "sms_default/bootstrap.css";
        $data["servers"] = $this->server_model->get_servers();


        if($page == "home"){
            $this->product_model = new \Product_model();
            $data['products'] = $this->product_model->get_products();
            //$data['products_entry'] = $this->product_model->get_products($id);
        }




        echo view('templates/header', $data);
        echo view('pages/'.$page, $data);
        echo view('templates/footer', $data);


    }


}



I'm routing my requests via "Routes.php"

PHP Code:
/**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.

$routes->get('products/(:segment)''Pages::renderProducts/$1');
$routes->match(['get''post'], 'admin/createProduct''Products::create');
$routes->get('/''Pages::index');
$routes->get('(:any)''Pages::renderOther/$1');

/**
 * --------------------------------------------------------- 


Is there a better way to do this?


Regards
The idea of a base controller is not very MVC.

In my shops I have controllers called 'departmenets' and 'products'. The departments controller calls the departments model that gets products for that department. The method is 'browse' so the url's are something like departments/browse/department_name.

For products the product controller calls a products model that gets all the product details, so the url for a product is something like products/show/product_name.

The home page has its own controller 'home' as that is usually a complex page.

Information pages like 'about us' and 'why shop' are done with an information controller.

The basket has a 'basket' controller and the checkout has a 'checkout' controller, etc.

Hope that helps.

Paul

PS Everyone does these things in different ways but do not have a 'base' controller for all your pages. For instance I have a render_page in my layouts library that deals with the headers and footers and specific elements of any page.
CI 4 comes with a BaseController already.
I think you can use hmvc pattern. It can help you to manage everything, in my project I created two controllers, BaseController and InterfaceController. I put them in app\Controllers then I set some default config.

I extends InterfaceController for handle interface request, and BaseController for do some action. Its also good for you if you can one class one function.

Simple doesn't mean for today, simple can be complex in another day Smile