I am learning PHP for couple of months. I can write a small blog or a simple cms. Can someone with more experience tell me if I'm doing things the right way?
I make view pages in this way :
<?php
$postFound = isset($onePost);
if ($postFound === false) {
$onePost = new StdClass();
$onePost->post_id = 0;
$onePost->title = "";
$onePost->post_text = "";
}
return
"<form method='post' action='admin.php?page=form' id='editor'>
<fieldset id='text-area'>
<legend>Create new post</legend>
<input type='hidden' name='id' value=$onePost->post_id>
<label>Title</label>
<input type='text' name='title' value=$onePost->title>
<label>Write post</label>
<textarea type='text' name='post-text'>$onePost->post_text</textarea>
<fieldset id='submit-area'>
<input type='submit' name='action' value='save'>
<input type='submit' name='action' value='delete'>
</fieldset>
</fieldset>
</form>";
This is my Front-Controller:
<?php
error_reporting(E_ALL);
ini_set("display_errors", true);
include_once "model/Layout.class.php";
$pageData = new Layout ();
$pageData->title = "Admin";
$pageData->addCss("assets/css/style.css");
$pageData->content = include_once 'view/admin/admin_navigation.html.php';
//navigation controll
$navClicked = isset($_GET['page']);
if ($navClicked) {
$link = $_GET['page'];
}else {
$link = 'posts';
}
$pageData->content .= include_once "controller/admin/$link.ctrl.php";
$layout = include_once "view/template/layout.html.php";
echo $layout;
I echo the view through an object property in Front-Controller. Is it good practice, to return html chunk of code like I do in the view file and then echo it? Or there is something bad going on behind the scenes which I am not awere? And Is there a better way?
Thanks in advance.
There's no reason to have this in a PHP tag and use
echo
. You can include it even if it's just raw HTML.