Jump to content

Search the Community

Showing results for tags 'mvc'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Welcome to PHP Freaks
    • Announcements
    • Introductions
  • PHP Coding
    • PHP Coding Help
    • Regex Help
    • Third Party Scripts
    • FAQ/Code Snippet Repository
  • SQL / Database
    • MySQL Help
    • PostgreSQL
    • Microsoft SQL - MSSQL
    • Other RDBMS and SQL dialects
  • Client Side
    • HTML Help
    • CSS Help
    • Javascript Help
    • Other
  • Applications and Frameworks
    • Applications
    • Frameworks
    • Other Libraries
  • Web Server Administration
    • PHP Installation and Configuration
    • Linux
    • Apache HTTP Server
    • Microsoft IIS
    • Other Web Server Software
  • Other
    • Application Design
    • Other Programming Languages
    • Editor Help (Dreamweaver, Zend, etc)
    • Website Critique
    • Beta Test Your Stuff!
  • Freelance, Contracts, Employment, etc.
    • Services Offered
    • Job Offerings
  • General Discussion
    • PHPFreaks.com Website Feedback
    • Miscellaneous

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


AIM


MSN


Website URL


ICQ


Yahoo


Jabber


Skype


Location


Interests


Age


Donation Link

Found 5 results

  1. I am quite new to php and the mvc setup, I am developing a library app however starting at the very basics so as not to become overwhelmed! I am trying to do a basic insert to my book table, this is the code I have so far alsong with the error I am presented with. Model (models > adminarea_model.php) adminarea_model.php public function create($title_text) { $title_text = strip_tags($title_text); $sql = "INSERT INTO book (title) VALUES (:title)"; $query = $this->db->prepare($sql); $query->execute(array(':title' => $title_text)); $count = $query->rowCount(); if ($count == 1) { return true; } else { $_SESSION["feedback_negative"][] = FEEDBACK_NOTE_CREATION_FAILED; } return false; } View (views > admin > addBook.php) addBook.php <form method="post" action="<?php echo URL;?>admin/create"> <label>Text of new note: </label><input type="text" name="title" /> <input type="submit" value='Create this note' autocomplete="off" /> </form> Controller (controllers > admin.php) admin.php public function create() { if (isset($_POST['title']) AND !empty($_POST['title'])) { $book_model = $this->loadModel('Admin'); $book_model->create($_POST['title']); } header('location: ' . URL . 'admin/addBook'); } When I am on admin/addBook and I try to submit the form I receive the following error; Fatal error: Call to a member function create() on a non-object in C:\xampp\htdocs\logintest\application\controllers\admin.php on line 43 Any ideas where I am going wrong? Line 43 contains the following $book_model->create($_POST['title']); Thanks J
  2. Hi everyone, I have been working through various tutorials over the past couple of months and am currently trying understand PHP frameworks. One of the ways I am doing this is by trying to design my own very simple MVC framework from scratch. I am trying to re-factor an application (which I have already built using spaghetti procedural PHP). This application has a front end for teachers and a back-end for the administrators. I would like to separate concerns and have URL's like this http://example.com/{module}/{controller}/{method}/{param-1}/{param-2} Now the MVC framework I have cobbled together up to this point does not handle routing for 'modules' (I apologise if this is not the correct terminology), only the controller/method/params. So I have separated the public_html from the app logic and inside of the /app/ folder I have specified two folders, my default "learn module" and the "admin module" so that the directory tree looks like this: Apparently this design pattern is a "H"MVC? My Solution I am basically making use if the is_dir(); function to check if there is a "module" directory (such as "admin") and then unsetting the first URL array element $url[0] and reindexing the array to 0... then I am changing the controller path according to the URL... the code should be clearer... <?php class App { protected $_module = 'learn'; // default module --> learn protected $_controller = 'home'; // default controller --> home protected $_method = 'index'; // default method --> index protected $_params = []; // default paramatars --> empty array public function __construct() { $url = $this->parseUrl(); // returns the url array // Checks if $url[0] is a module else it is a controller if (!empty($url) && is_dir('../app/' . $url[0])) { $this->_module = $url[0]; // if it is a model then assign it unset($url[0]); if (!empty($url[1]) && file_exists('../app/' . $this->_module . '/controllers/' . $url[1] . '.php')) { $this->_controller = $url[1]; // if $url[1] is also set, it must be a controller unset($url[1]); $url = array_values($url); // reset the array to zero, we are left with {method}{param}{etc..} } // if $url[0] is not a module then it might be a controller... } else if (!empty($url[0]) && file_exists('../app/' . $this->_module . '/controllers/' . $url[0] . '.php')) { $this->controller = $url[0]; // if it is a controller then assign it unset($url[0]); $url = array_values($url); // reset the array to zero } // else if url is empty default {module}{controller}{method} is loaded // default is ../app/learn/home/index.php require_once '../app/' . $this->_module . '/controllers/' . $this->_controller . '.php'; $this->_controller = new $this->_controller; // if there are methods left in the array if (isset($url[0])) { // and the methods are legit if (method_exists($this->_controller, $url[0])) { // sets the method that we will be using $this->_method = $url[0]; unset($url[0]); } // else nothing is set } // if there is anything else left in $url then it is a parameter $this->_params = $url ? array_values($url) : []; // calling everything call_user_func_array([$this->_controller, $this->_method], $this->_params); } public function parseUrl() { // checks if there is a url to work with if (isset($_GET['url'])) { // explodes the url by the '/' and returns an array of url 'elements' return $url = EXPLODE('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL)); } } } this so far appears to be working for me, but..... Question I am not sure if this is the preferred solution to this issue. Is calling the is_dir() check for every page request going slow down my app? How would you engineer a solution or have I completely misunderstood the issue? Many thanks in advance for your time and consideration!! Hazel,
  3. I have followed a youtube playlist to build a simple MVC framework based on PHP, here is a link to the playlist I have followed. and here is an image of my application structure image I have connected the application to a MySql database, everything is working fine, but now I am trying to do friendly URLs. Currently, my URLs look like the following: mywebsite.com/public/home/property This is fine as this link is for a static page, but I want the same for dynamic URLs, for example: Current URL: mywebsite.com/public/home/property?id=10 to URL: mywebsite.com/public/home/property/id/10 I have tried a lot of methods in htaccss but the data is not pulled from the database once the URL is re-written and I get only an empty page. current .htaccess code (the one that is located in the public folder): Options -MultiViews RewriteEngine On RewriteBase /public RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] I have tried a lot of methods and rules but non have worked. I am not sure if the details I provided are clear enough, I am little confused as I combined a lot of tutorials together to create this app. App.php code: <?php class App{ protected $controller = 'home'; protected $method = 'index'; protected $params = []; public function __construct(){ $url = $this->parseUrl(); if(file_exists('../app/controllers/' . $url[0] . '.php')){ $this->controller = $url[0]; unset($url[0]); } require_once '../app/controllers/' .$this->controller. '.php'; $this->controller = new $this->controller; if(isset($url[1])){ if(method_exists($this->controller, $url[1])){ $this->method = $url[1]; unset($url[1]); } } $this->params = $url ? array_values($url) : []; call_user_func_array([$this->controller, $this->method], $this->params); } public function parseUrl(){ if(isset($_GET['url'])){ return $url = explode('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL)); echo $url; } } } ?> controller.php code: <?php class Controller{ public function model($model){ require_once '../app/models/' . $model . '.php'; return new $model(); } public function view($view, $data = []){ require_once '../app/views/' . $view . '.php'; } } ?> controllers code: <?php class home extends Controller{ public function index(){ $this->view('home/index'); } public function listings(){ $this->view('home/listings'); } public function property(){ $this->view('home/property'); } } ?> code for the page that should show data from mysql database: <?php include "includes/head.php"; ?> <?php $property = Property::find_id($_GET['id']); ?> <section id="sub-main"></section> <section id="sub-list"> <div class="container"> <div class="row"> <div class="col-xs-12 col-sm-6"> <div id="listingCarousel" class="carousel slide" data-ride="carousel"> <div class="carousel-inner" role="listbox"> <div class="item active"> <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=585%C3%97380&w=585&h=380" alt=""> </div> <div class="item"> <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=585%C3%97380&w=585&h=380" alt=""> </div> <div class="item"> <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=585%C3%97380&w=585&h=380" alt=""> </div> </div> </div> </div> <div class="col-xs-12 col-sm-6"> <h1><?php echo $property->property_name; ?></h1> <ul class="list-inline"> <li><i class="fa fa-map-signs" aria-hidden="true"></i> <b></b> <?php echo $property->city; ?></li> </ul> </div> </div> <hr> <div id="map"></div> </div> </section> <?php include "includes/subscribe.php"; ?> <?php include "includes/footer.php"; ?> The project is online, I can give full access if someone interested in helping me solve this issue.
  4. Hi all, I have just put together a tentative proof of concept MVC framework proposal. After many weeks of slogging through an array of PHP MVC tutorials and courses (some of which I paid for) I have decided to strip back all the frills, bells and whistles to try and put together a core proof of concept that uses best practices. In this simplified example I am trying to test the viability of the framework itself... so there is no routing code or query string management and the controller and action are hard coded. Here is the repository: https://github.com/JethroHazelhurst/psr-4-mvc I like this framework because it: Has clear seperation of concerns Is not cluttered with static functions Is clearly namespaced - thanks to the PSR-4 autoloading Has a clear way of passing data to and from the model - e.g. $this->_view->foo = $this->_model->getBar(); Below is the directory structure: and here is a print_r of my Object structure: Questions I am interested in hearing any feedback on the idea... I can't see any glaring issues at them moment. Some questions in the back of my mind are: Are there any dangers in heavily depending on the parent::__construct() function to call parent classes? In the controller, is passing data from Model to View using $this->_view->foo = $this->_model->getBar(); good practice? Are there any dangers in using Namespaces? I am also interested in reading up on MVC framework best practices so if anyone can recommend some resources I would be very grateful. It seems like there are a million ways to write an MVC framework, I am confused as to which way is best practice. EDIT: Hmm, can't seem to get a list to display here...
  5. Hi there, I'm learning PHP and working on building an ecommerce site using a MVC. I can display all six categories from the database on the index page however I only want to show four of these, as the links at the top will be the six categories. By using the next() method I can display all six categories: <?php while ($categories->next()) { ?> <section class="product"> <a href="products/<?= $categories->getURL() ?>"><h3 class="upper"><?= $categories->getName() ?></h3></a> <br/> <img src="<?= IMAGE_URL . $categories->getImage() ?>" style="width:80%; height:auto;" alt="Menu Tab"/> <h3 class="under"><a href="products/<?= $categories->getURL() ?>">SHOP NOW >></a></h3> </section> <?php } } } ?> I am wondering how to only extract certain products (each has a catID in the database). I thought the switch statement might be the answer and here was my attempt that did not work. I guess I am still not saying which catID to get but unsure how to fix this. Or is there another way not using switch? while ($categories->next()) { switch ($categories->getID()) { case 1: case 2: case 3: case 4: ?> <section class="product"> <a href="products/<?= $categories->getURL()?>"><h3 class="upper"><?=$categories->getName() ?></h3></a> <br/> <img src="<?=IMAGE_URL. $categories->getImage()?>" style="width:80%; height:auto;" alt="Menu Tab"/> <h3 class="under"><a href="products/<?= $categories->getURL()?>">SHOP NOW >></a></h3> </section> <?php break; default: return ""; } } } } ?> Any help would be greatly appreciated! Cheers
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.