How is the best way to show my content in architecture MVC? Actually I'm doing the following way:
<?php
class view {
function __construct(){
}
function __set($var,$value){
$this->var = $value;
}
function __get($var){
return $this->var;
}
function render($render, $noinclude = false){
if($noinclude == true){
require ("view/template/".$render.".php");
}
else{
require ("view/template/headerTPL.html");
require ("view/template/bannerTPL.html");
require ("view/template/menuTPL.html");
require ("view/template/".$render.".php");
require ("view/template/footerTPL.html");
}
}
function show($value){
$this->value = $value;
}
function alert($value){
echo "<script>alert('{$value}')</script>";
}
}
?>
<div id="conteudo">
<?php require $this->menu; ?>
</div>
This is my render.php /\
Is it wrong to include the TPL? How should I do it?
Your separating your HTML code into template files is on track, and a good way to do things. I see some HTML at the bottom of this file. I would recommend you move that into a template as well.
Just as a general guideline, the main logic of your program (controller) should never have any HTML created from it. It should pass the data (model) to the template (view) in an pure and unformatted state, then the template code will format it properly.
If you want to speed up the development of MVC programs, I would recommend you check out the CodeIgniter framework. http://codeigniter.com/
Related
I am creating website in PHP. I am using MVC in PHP. My website works like this, if user go to example.com/about then it it will load About class and index() function. If user will go to localhost/about/founder then it will load founder() function from About class. but the thing is that if I go to localhost/About or localhost/AbOut or anything like that it is loading default index() function from About class file. So what to do with case sensitivity? I mean I want my script to load index() function from class file if it is localhost/about or localhost/terms. If anything is in uppercase, then it should load 404 error function. 404 error function is already set in my site.
Please help me friends.
here is my Bootstrap.php class file
<?php
/*
Bootstrap class to run functions by URL
*/
class Bootstrap {
public $_req;
public $_body;
public $_file;
public $_error;
function __construct(){
if(empty($_GET['req'])){
require 'classes/home.php';
$this->_body = new Home();
$this->hdr($this->_body->head());
$this->_body->index();
$this->ftr();
exit();
}
$this->_req = rtrim($_GET['req'], '/');
$this->_req = explode('/', $this->_req );
$_file = 'classes/'.$this->_req[0].'.php';
if(file_exists($_file)){
require $_file;
}
else {
$this->error(404);
}
$this->_body = new $this->_req[0];
$this->hdr($this->_body->head());
if(isset($this->_req[2])){
if(method_exists($this->_req[0], $this->_req[1])){
$this->_body->{$this->_req[1]}($this->_req[2]);
}else {
$this->error(404);
}
}else {
if(isset($this->_req[1])){
if(method_exists($this->_req[0], $this->_req[1])){
$this->_body->{$this->_req[1]}();
}else {
$this->error(404);
}
}else {
$this->_body->index();
}
$this->ftr();
}
}
//this function is to set header in html code
public function hdr($var = false){
echo '<!DOCTYPE HTML><html><head>'.$var.'</head><body>';
}
//this function is tp set footer in html code
public function ftr($var = false){
echo $var.'</body></html>';
}
//error handler
public function error($var){
require 'classes/er_pg.php';
$this->_error = new Error();
$this->_error->index($var);
}
}
You shouldn't use anything to load non-lowercase URLs because of the duplicate content, and that's a good thing you're doing. The wrong URLs should fail automatically in such cases.
However, since you didn't show how are you making those calls, then only thing I can suggest at this point is to check if the called method exists (case-sensitive), and if not, throw/redirect to a 404 page (header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");).
UPDATE
After all the chat in the comments, seems like file_exists is not case-sensitive in your case, which is really weird. Hopefully someone will be able to figure it out so I can delete this (keeping it because of the info in the comments).
I solved the problem. I used this
if(ctype_lower($this->_req[0])){
$_file = 'classes/'.$this->_req[0].'.php';
and now its working. Thanx anyways friends.
Is it possible to call only the specific function from another file without including whole file???
There may be another functions in the file and don't need to render other function.
The short answer is: no, you can't.
The long answers is: yes, if you use OOP.
Split your functions into different files. Say you are making a game with a hero:
Walk.php
function walk($distance,speed){
//walk code
}
Die.php
function die(){
//game over
}
Hero.php
include 'Walk.php';
include 'Die.php';
class Hero(){
//hero that can walk & can die
}
You may have other functions like makeWorld() that hero.php doesn't need, so you don't need to include it. This question has been asked a few times before: here & here.
One of the possible methods outlined before is through autoloading, which basically saves you from having to write a long list of includes at the top of each file.
In PHP it's not available to get only a little part of a file.
Maybe this is a ability to use only little parts of a file:
I have a class that calls "utilities". This I am using in my projects.
In my index.php
include("class.utilities.php")
$utilities = new utilities();
The file class.utilities.php
class utilities {
function __construct() {
}
public function thisIsTheFunction($a,$b)
{
$c = $a + $b;
return $c;
}
}
And then i can use the function
echo $utilities->thisIsTheFunction(3,4);
include a page lets say the function is GetPage and the variable is ID
<?php
require('page.php');
$id = ($_GET['id']);
if($id != '') {
getpage($id);
}
?>
now when you make the function
<?php
function getpage($id){
if ($id = ''){
//// Do something
}
else {
}
}
?>
I was wondering if someone can show me a good way to handle errors in my PHP app, that i am also easily able to reuse in my other scripts.
So far i have been using the following functions:
Inline Errors
function display_errors_for($fieldname) {
global $errors;
if (isset($errors[$fieldname]))
{
return '<label for="' .$fieldname. '" class="error">' . ucfirst($errors[$fieldname]). '</label>';
} else {
return false;
}
}
All Errrors
function display_all_errors($showCounter = true) {
global $errors;
$counter = 0;
foreach ($errors as $errorFieldName => $errorText)
{
if ($showCounter == true)
{
$counter++;
echo '<li>' . $counter . ' - <label for="' .$errorFieldName. '">' .$errorText. '</label></li>';
} else {
echo '<li><label for="' .$errorFieldName. '">' .$errorText. '</label></li>';
}
}
}
I have a $errors = array(); defined on the top of my global file, so it is appended to all files.
The way i use it is that, if i encounter an error i push a new error key/value to the $errors array holder, something like the following:
if (strlen($username) < 3) {
$errors['username'] = "usernames cannot be less then 3 characters.";
}
This all works great and all, But i was wondering if some one has a better approach for this? with classes? i don't think i want to use Exceptions with try/catch seems like an overkill to me.
I'm planning to make a new app, and i'll be getting my hands wet with OOP alot, though i have already made apps using OOP but this time i'm planning to go even deeper and use OOP approach more extensively.
What i have in mind is something like this, though its just a basic class i will add further detail to it as i go deeper and deeper in my app to see what it needs.
class Errors
{
public $errors = array();
public function __construct()
{
// Initialize Default Values
// Initialize Methods
}
public function __destruct()
{
//nothing here too...
}
public function add($errorField, $errorDesc)
{
if (!is_string($errorField)) return false;
if (!is_string($errorDesc)) return false;
$this->errors[$errorField] = $errorDesc;
}
public function display($errorsArray)
{
// code to iterate through the array and display all errors.
}
}
Please share your thoughts, if this is a good way to make a reusable class to store and display errors for an entire app, or is getting more familiar with exceptions and try/catch my only choice?
I cannot imagine a reason for creating a distinct class for as trivial task as echoing several text strings into browser.
Or I am missing something.
I see no point for the function even.
Personally I have in my templates (where I need it) the code block like this
<? if ($data['errors']): ?>
<div class="errors">
<ul>
<? foreach ($data['errors'] as $e): ?>
<li><?=$e?></li>
<? endforeach ?>
</ul>
</div>
<? endif ?>
and find it quite enough
If you are into OOP, you should use framework like Zend Framework. In ZF there is an action helper for general error/result messages: FlashMessenger
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.flashmessenger
Question Updated
I am building an MVC framework, for my templates and views, I will have a main page template file and my views will be included into this template.
The only way I have seen to do this is to use output buffereing
ob_start();
include 'userProfile.php';
$content = ob_get_clean();
Is there any other way of doing this? I think output buffering is not the best on performance as it uses a lot of memory
Here is a sample controller, the $this->view->load('userProfile', $profileData);
is the part that will be loaded using output biffering so that it can be included into the main template below into the $content part
view class
public function load($view,$data = null) {
if($data) {
$this->data = $data;
extract($data);
} elseif($this->data != null) {
extract($this->data);
}
ob_start();
require(APP_PATH . "Views/$view.php");
$content = ob_get_clean();
}
controller
/**
* Example Controller
*/
class User_Controller extends Core_Controller {
// domain.com/user/id-53463463
function profile($userId)
{
// load a Model
$this->loadModel('profile');
//GET data from a Model
$profileData = $this->profile_model->getProfile($userId);
// load view file
$this->view->load('userProfile', $profileData);
}
}
main site template
<html>
<head>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
Using a template system is not necessarily tied to output buffering. There are a couple of things in the example code you give that should certainly not be taken for granted:
One:
flushblocks(); // what does this do??
And two:
$s = ob_get_clean();
Why does the code capture the template output into a variable? Is it necessary to do some processing on this before outputting it? If not, you could simply lose the output buffering calls and let the output be sent to the browser immediately.
All,
I am studying some sample code given in my web dev class as an example of MVC (again, for the web). In this code, there's a system to navigate from the index.php page to the various controllers (which then call the Model and View modules), and then back into index.php.
I understand how the MVC works.
What I'm grappling with is the navigation mechanism. I am having difficulties understanding how all the pieces work together.
Could anyone take a look at the code below and tell me if this matches a well known method / pattern to deal with dynamic website navigation? (Maybe the Front Controller?) If it does, then my hope is that I can more easily do some more research on it.
Many thanks!
JDelage
Index.php
<?php
require_once("User.php");
session_start();
if (isset($_GET['action']))
$action= $_GET['action'];
else
$action="";
switch ($action) {
case 'login':
require_once('Login.php');
$command= new LoginControler();
break;
case 'logoff':
require_once('Logoff.php');
$command= new LogoffControler();
break;
// Several other cases
default:
require_once('Unknown.php');
$command= new UnknownControle();
}
$command->execute();
require_once('EntryMenu.php'); // Those are objects that represent both the
// menu label and the links.
$menu= array(
new EntryMenu("Login", "index.php", array("action" => "logon")),
new EntryMenu("Logoff", "index.php", array("action" => "logoff")),
new EntryMenu("Write", "index.php", array("action" => "write")),
new EntryMenu("Read", "index.php", array("action" => "read"))
);
if ($command->redirect) {
header('Location: ' . $command->redirect);
} else if ($command->page) {
include("ui/header.php");
include("ui/menu.php");
echo "<div class='content'>";
include("ui/". $command->page);
echo "</div>";
include("ui/footer.php");
}
?>
Controler.php
<?php
class Controler {
public $page= "problem.php";
function execute() {}
}
?>
LogoffControler.php
<?php
require_once('Controler.php');
class LogoffControler extends Controler {
function execute() {
$this->redirect= "index.php";
unset($_SESSION['user']);
}
}
?>
LoginControler.php
<?php
require_once('LoginModel.php'); // This manages the exchanges with the user db
require_once('Controler.php');
class ConnexionControle extends Controler {
public $page= "LoginForm.php";
function execute() {
// More code to deal with incorrectly filled login forms
$login = new LoginModel();
$login->loginUser($_POST['login'], $_POST['password']);
if ($login->userLogedIn()) {
$_SESSION['user']= $login->user;
$this->redirect= "index.php";
}
// More code to deal with invalid logins
}
}
?>
I am assuming you understand the controller part, and is asking about the switch..case statements. I haven't come across an official name for that yet,but most MVC frameworks for PHP (Kohana, CakePHP, CodeIgniter, Fat Free and etc.) calls that 'routing'. It's mapping of a URL to a controller.
Using a switch..case sets of statement is one of the easier ways. More sophisticated solutions use RegEx to match pre-defined URL patterns to resolve what controller to invoke, and what are its parameters (usually bundled as a 'request')
Other methods include using URL rewriting to come up with pretty urls, such as /articles/month/nov/article-id/3
which in 'ugly url form' is :
action=articles&month=nov&article-id=3
If you would like an easy-to-dissect verion of a MVC system you could try the 1kb PHP MVC which handles everything you are attempting in a much cleaner fashion. Though you might have to break up the code if you really want to read it as it is in compressed form.
With this system you simply place a controller in /classes/controller/ named somthing.php and you can then access it from the URL like http://site.com/something.
Loading Models is also easy and doesn't require any include or require calls.
class Controller_Something
{
public function index()
{
$model = new Model_User();
}
}