Jump to content


trq

Member Since 31 Mar 2005
Offline Last Active Oct 22 2014 06:57 AM

#1492446 MVC, how to approach a problem

Posted by trq on 30 September 2014 - 07:02 AM

You have a few questions rolled into a single post here. I'll try to answer as many of them as i can inline then maybe provide an example of sorts.
 

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.


This is pretty much spot on. You would "model" your part using a class.
 

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.


You should try and keep the methods in your controllers as small as possible. Most of the logic should be abstracted out into other objects. Avoid interacting with "models" within your controllers. While it may seem like the easiest option, it is also the route that leads to code duplication.

At minimum there should be a "repository" between your controllers and the models. You can then ask the repository to get whatever data you need.
 

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.


You need multiple views. You are displaying multiple types of data.
 

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've already answer the first two of these and the second two don't really apply/make a lot of sense.

I'm going to try and build a very simple example, using mostly pseudo type code. How this the controllers are called and that sort of thing would really depend on the framework you are using. If you're not using a framework, you're doing it wrong. Find one and learn it.

Model
<?php

class Part
{
  protected $id;
  protected $name;
  protected $description;

  public function setId($id)
  {
    $this->id = $id;
  }

  public function getId($id)
  {
    return $this->id;
  }

  public function setName($name)
  {
    $this->name = $name;
  }

  public function getName($name)
  {
    return $this->name;
  }

  public function setDescription($description)
  {
    $this->description = $description;
  }

  public function getDescription($description)
  {
    return $this->description;
  }
}
Repository
<?php

class PartRepository
{
  public function findById($name)
  {
    // some logic to find a Part by id and return it
  }

  public function findByName($name)
  {
    // some logic to find a Part by name and return it
  }

  public function all()
  {
    // some logic to get all parts
    // returns a collection of Part objects
  }

  public function findLike($name)
  {
    // some logic to find Parts where name like $name
    // returns a collection of Part objects
  }

  public function persist(Part)
  {
    // some logic to save a Part to the database
  }
}
The Controller
<?php

class PartsController
{
  protected $view;
  protected $repo;

  public function __construct(View $view, PartRepository $repo)
  {
    $this->view = $view;
    $this->repo = $repo;

    $this->view->setLayout('layout.html.php');
  }

  /**
   * Used to display a list of all parts
   */
  public function index()
  {
    $parts = $this->repo->all();
    return $this->view('list.html.php', $parts);
  }

  /**
   * Used to display a single part
   */
  public function show($id)
  {
    $part = $this->repo->findById($id);
    return $this->view('show.html.php', $part);
  }

  /**
   * Used to search for a list of parts
   */
  public function search($name)
  {
    $parts = $this->repo->findLike($name);
    return $this->view('list.html.php', $part);
  }
}
layout.html.php
<html>
  <head>
  </head>
  <body>
    <?php echo $content; ?>
  </body>
</html>
list.html.php
<ul>
<?php foreach ($parts as $part): ?>
  <li><a href="/<?php echo $part->getId(); ?>"><?php echo $part->getName(); ?></a></li>
<?php endforeach; ?>
</ul>
show.html.php
<h2><?php echo $part->getName(); ?></h2>
<p><?php echo $part->getDescription(); ?></p>
Like I said, a VERY simple example. But hopefully it's enough to let you easily see the different layers, and to get an idea about how they might interact. I didn't implement any search or filter, I'm hoping that shouldn't be too hard to see how that would simply be another method on the controller though.


#1492437 Rewrite WWW to https Problem

Posted by trq on 30 September 2014 - 02:10 AM

I've never seen rewrite rules like this. What kind of server is this used on?


This is the IIS board.


#1492258 How to load pages within a template properly

Posted by trq on 27 September 2014 - 08:19 PM

Instead of re-inventing your own templating system, why not use one that already exists?

You could start with something simple like Plates (which is just a few simple helpers) or a complete templating engine like Twig (which requires you learn a new syntax). Either way, people have already resolved the issues that you are running into, in far more elegant ways.


#1490559 PHP Hashing - improve

Posted by trq on 10 September 2014 - 01:19 AM

How would you improve the hashing?


I would remove all of your code and use password_hash as it was intended.


#1489719 PHP code to trigger file download

Posted by trq on 03 September 2014 - 12:27 AM

This example should get you started:

http://forums.phpfre...orce-downloads/


#1489366 Functions Conflicting

Posted by trq on 30 August 2014 - 08:24 AM

Hi I found a very old outdated script for temporary banning people.


So why are you trying to use it? All of the above code needs to be thrown away.


#1486884 About 2:1 banner exchange sites and bots

Posted by trq on 05 August 2014 - 10:56 AM

Of course bots have ip addresses.


#1486794 Please help me ! i need 1 website bussiness

Posted by trq on 04 August 2014 - 08:06 AM

What the actual fuck?


#1486288 arrays in C vs arrays in PHP

Posted by trq on 28 July 2014 - 12:16 AM

Who cares?


#1485972 How often do you use Interfaces?

Posted by trq on 23 July 2014 - 01:16 AM

It's pretty hard to write SOLID code without interfaces.


#1485550 Website Server was upgraded to PHP 5.4.30 and now doesn't work

Posted by trq on 17 July 2014 - 10:32 AM

That code is in some serious need of being thrown out.

The session_is_registered function is deprecated.
The mysql_* extension is deprecated
Short tags have long been disabled by default.

And in general it's just poor code. Moving forward, you might want the code maintained.


#1484623 Are 'registry' classes good practice?

Posted by trq on 10 July 2014 - 09:14 PM

As long as this registry is injected via __constructs I don't see it as such a bad thing. It is basically a IoC container in that case.

Just make sure that you are type hinting to a well defined interface for this Registry.

The registry pattern has the potential to cause issue when you make it a singleton that has a static getInstance() method or similar.


#1483771 Specific Criticism of CodeIgniter as a PHP Framework in 2014

Posted by trq on 04 July 2014 - 12:21 AM

I'm not sure how many valid opinions you're going to get around here. I for one have never and would have never even considered using CI. As far as I am concerned the project can't die soon enough.

I understand it was popular amongst a certain crowd of developers, and I understand that for a lot of instances it has filled a need. I also believe however that overall it has probably done more harm than good to the php landscape.

An "Abomination" is the latest term I've been using to describe it. The sooner it is gone, the better IMO.


#1483062 git with master branch only?

Posted by trq on 23 June 2014 - 07:34 AM

It is a "to each his own" type thing and no, I don't see any issue with leaving out the develop branch. I have worked at places and on projects using master & develop, master, staging and develop and master alone, it really is up to you. With fewer devs (I have found), it probably makes more sense not to worry about develop.

Make sure to always use feature branches however.


#1482763 Fatal error: Call to undefined function filter_var() on line 186 in ump .clas...

Posted by trq on 17 June 2014 - 01:20 AM

It doesn't yet exist. You'r using a terribly old version of php (nearly 8 years old, released 24-Aug-2006).

As stated in the manual, filter_var didn't exist until 5.2.




Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com