Jump to content


Member Since 31 Mar 2005
Offline Last Active Apr 17 2017 05:38 AM

#1500332 getting $_GET in a framework

Posted by trq on 22 December 2014 - 03:30 AM

The point I'm trying to make is why have the rewrite at all if you're just going to use normal querystring parameters? I understand you want to force everything through a front controller, but why stop there?

But yeah, you could just use the QSA flag to have apache append existing parameters.
RewriteRule ^(.*)$ index.php?uri=$1 [PT,L,QSA]

#1496130 Cronjob and insert content in site

Posted by trq on 08 November 2014 - 10:52 PM

Your script is going to have to write to the index.php file.
Easy way is to make a template file with a searchable tag you can do a find and replace on like: {{replace_text}}
You open the file, search and replace the tags you want.
Will need to set the permissions on that file to make it writeable.
Then use functions like:
preg_match() or str_replace()

Why would you edit the index.php file? PHP is a programming language, you can use it to dynamically display data from different data sources. Having cron dynamically alter the PHP "script" is ridiculous.

Instead, have your cron job put the data somewhere PHP can easily access it (like a database) and then write some logic into your php script to retrieve this data and display it.

#1494673 How to load a PHP class into a PHP functions

Posted by trq on 25 October 2014 - 07:10 AM

doing what wrong?


#1492446 MVC, how to approach a problem

Posted by trq on 30 September 2014 - 12:02 PM

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.

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.


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.


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.


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;

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

class PartsController
  protected $view;
  protected $repo;

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


   * 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);
    <?php echo $content; ?>
<?php foreach ($parts as $part): ?>
  <li><a href="/<?php echo $part->getId(); ?>"><?php echo $part->getName(); ?></a></li>
<?php endforeach; ?>
<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 - 07: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 28 September 2014 - 01:19 AM

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 - 06: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 - 05:27 AM

This example should get you started:


#1489366 Functions Conflicting

Posted by trq on 30 August 2014 - 01:24 PM

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 - 03:56 PM

Of course bots have ip addresses.

#1486794 Please help me ! i need 1 website bussiness

Posted by trq on 04 August 2014 - 01:06 PM

What the actual fuck?

#1486288 arrays in C vs arrays in PHP

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

Who cares?

#1485972 How often do you use Interfaces?

Posted by trq on 23 July 2014 - 06: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 - 03:32 PM

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 11 July 2014 - 02:14 AM

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.