frontend OOPS

Now, you got the idea how we can work on object oriented programming in the backend. Let's go to the frontend then which should be far more easier now.


We can duplicate the main.php file we created for our backend application and modify the path directly for configs.php and autoload.php. Rest of the things will be same and the same error page will work for us that we created while working on the application part. We can separate the header and footer section of our frontend files and include them in main.php as we did for the dashboard.



$page = isset($_GET['page']) ? $_GET['page'] : 'home';
$title = ucfirst($page);
$page .= '.php';

include ROOT.'layouts/header.php';
$fileName = ROOT.$page;

if(file_exists($fileName) && (is_file($fileName))){
	require_once ROOT.'Errors/404.php';
include ROOT.'layouts/footer.php';


As we did earlier, here too, we'll need an index.php file that redirects the users to main.php first and then load the requested page. In this example, we set up home.php as our landing page.

header('Location: main.php?page=home');	


Now, the only part left would be to fetch the data from database and display in our frontend view files. We already had our autoload file in main.php so, we'll just call those classes in our frontend files and use the functions from there whenever we require.

$cats = new category();
$catNames = $cats->getCategory();

We know, we've got a function named getCategory() in our category.php controller file. So, we'll call the same controller in our header.php file and retrieve and display all those category names in the menu section.

<ul class="nav nav-justified fit">
    <li><a href="<?=HTTP?>">Home</a></li>
    <?php foreach($catNames as $catName): ?>
        <li><a href="main.php?page=category&id=<?=$catName->id?>"><?=$catName->name?></a></li>
    <?php endforeach; ?>

The first link in the menu list is a static one because we always want it to redirect the user to the landing page and the ROOT constant will always takes us there.

For all the category names inserted in the categories table, we used the same variable we generated on top to get those names and reiterated in foreach loop so that each of the caegory names get printed in the menu list.

We'll send all the page requests from our category links to a file named category.php where data will be displayed by retrieving all the news data from the news table where id will be the same as of the category being opened. We did it in the procedural php part too.

Same will be the case with each of the sections and data in index page and category while single page will display the single news data in details where we can display related data as well as the desired ones to in some sections generally the sidebar.

We can create multiple functions to display data in order or by limit too whenever required.

0 Like 0 Dislike 0 Comment Share

Leave a comment