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


  • 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


Last Updated

  • Start


Filter by number of...


  • Start





Website URL








Donation Link

Found 7 results

  1. Hi everyone, So I'm relatively new to OO PHP and moreover, OO PHP with MVC design pattern. This may be a largeish post so please bear with me on this one! So here is a scenario that I'd like to understand. There are likely multiple ways to go about this, but it'd be nice to see what is said. I'll include what I think should be the solution here and hopefully i'll get some feedback about it. Scenario: A page needs to display a list of "parts" for a car. A database table already exists with these parts. The list of parts on the page need to be ordered by name on first load, but then can be re-ordered by users using a drop down list. They can also be filtered, and searched. A page also exists to display a single car part. What I think should be, and what i'm struggling with: Model: I will have a "part" object which represents an individual car part. The part object will use a database abstraction layer. On "new Part()" will generate an empty object. ->Load( id ) will load an individual part. Controller: I do not know how I would implement this. I know it would contain methods to Filter(), Search() and Order() and that would directly access the Model. View: I am lost here too, I need to display a list of car parts, and on another page, a single car part. I understand I should use the same Model for both. However I do not see how I would list the parts. Some Questions: Should I have another "model" that is a list of the "Part" model called PaetListModel, or should "Part" be able to generate that list? I clearly should have 2 views, one for singular part, one for list of parts. Should the view access the model to generate the list before using the data for output? Should the controller be used in the view instead of the model to generate the initial list (or singular) on page load? Should the filter functions in the controller reload the "PartsList" from wherever our list is stored? I think the most important question for me though is: How would YOU implement the above green scenario? I would like to learn from peoples examples so I get an idea of what road to follow
  2. johnnys

    Basic MVC Issue

    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
  3. 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,
  4. 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.
  5. 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...
  6. 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
  7. kringeltorte

    Prepared Statement in PHP MVC

    I am trying to create a simple forum in a MVC architecture. This is my database setup (the relevant part): Table: forum_categories `forum_categories` ( `cat_id` INT( NOT NULL AUTO_INCREMENT, `cat_title` VARCHAR(255) NOT NULL, `cat_desc` TEXT NOT NULL, PRIMARY KEY (`cat_id`), UNIQUE KEY (`cat_title`) Table: forum_topics `forum_topics` ( `topic_id` INT( NOT NULL AUTO_INCREMENT, `cat_id` INT( NOT NULL COMMENT 'foreign key with forum_categories table', `user_id` INT(11) NOT NULL COMMENT 'foreign key with users table', `topic_title` VARCHAR(255) NOT NULL, `topic_desc` TEXT NOT NULL, `topic_date` DATETIME DEFAULT NULL, PRIMARY KEY (`topic_id`), FOREIGN KEY (`cat_id`) REFERENCES forum_categories (`cat_id`) ON DELETE CASCADE ON UPDATE CASCADE Example of the functionality, I would like to achieve: Category 1 has cat_id = 1 Category 2 has cat_id = 2 Topic 1 has cat_id = 1 Topic 2 has cat_id = 2 Now when category 1 is selected I just want topic 1 to show. If category2 is selected I just want topic 2 to show. This prepared SQL statement achieves that: PREPARE stmnt FROM 'SELECT * FROM forum_categories fc JOIN forum_topics ft ON fc.cat_id = ft.cat_id WHERE fc.cat_id = ? ORDER BY ft.topic_date DESC'; SET @a = 1; EXECUTE stmnt USING @a; My Problem: I would like to move this functionality into my PHP MVC structure. Here is my attempt, which does not work (it shows all topics in all categories). Controller /** * Show all the topics in the chosen category */ public function showForumTopics() { $topic_model = $this->loadModel('Forum'); $this->view->forum_topics = $topic_model->getForumTopics(); $this->view->render('forum/viewTopics'); } Model /** * Gets an array that contains all the forum topics in the database. * Each array element is an object, containing a specific topic's data. * @return array All the forum topics */ public function getForumTopics($cat_id) { $sql = 'SELECT * FROM forum_categories fc JOIN forum_topics ft ON fc.cat_id = ft.cat_id WHERE fc.cat_id = :cat_id ORDER BY ft.topic_date DESC'; $query = $this->db->prepare($sql); $query->execute(array(':cat_id' => $cat_id)); return $query->fetchAll(); } View if ($this->forum_topics) { foreach($this->forum_topics as $key => $value) { echo '<p><strong>Title:</strong>' . $value->topic_title . '</p>'; echo '<p><strong>Description:</strong> ' . $value->topic_desc . '</p>'; echo '<p><strong>Author:</strong> ' . $value->topic_author . '</p>'; echo '<p><strong>Date:</strong> ' . $value->topic_date . '</p>'; } } else { echo 'No forum topics.'; } Help would be highly appreciated! Thank you!!

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.