/php-mvc-base

A structure I use to get started with PHP projects. It enables the developer to hook up plugins and let's you use pretty URLs

Primary LanguagePHP

Important note!

Versions 0.3.0, 0.3.1 and 0.3.2 are broken.

Please switch to version 0.3.3 if you experience any problems

About

A structure I use to get started with PHP projects.

It enables the developer to hook up plugins and let’s you use pretty URLs

Test it!

  • To test this project, just download the zip (or tar.gz, or fork it etc).
  • Place it in your web servers root (remember to include the .htaccess file!).
  • start your web server and head to: http://localhost/web/test

Usage

I’ve added some README files in the various folders.

The web/index.php is the heart of the system.
This means that your web applications root folder is the “web” folder.

All requests go through this file and it decides how the routing of the app
should be.
You can add additional hooks in this file to add certain routes etc (a better
system for this will arrive one day).

Structure

The root of the project holds a few directories:
/app – This is the folder where your magic will happen. Use the views, controllers and models folder for your app code.
/config – this folder holds a few configuration files. Currently only the connection to the database.
/lib – This is where you should put external libraries and other external files.
/lib/base – The library files. Don’t change these :)
/web – This folder holds files that are to be “downloaded” from your app. Stylesheets, javascripts and images used. (and more of course)

The system uses a basic MVC structure, with your web app’s files located in the
“app” folder.

app/controllers

Your application’s controllers should be defined here.

  • All controller names should end with “Controller”. E.g. TestController.
  • All controllers should inherit the library’s “Controller” class.

However, you should generally just make an ApplicationController, which extends
the Controller. Then you can defined beforeFilters etc in that, which will get run
at every request.

app/models

Models handles database interaction etc.

  • All models should inherit from the Model class, which provides basic functionality.

The Model class handles basic functionality such as:

  • Setting up a database connection (using PDO)
  • fetchOne(ID)
  • save(array) → both update/create
  • delete(ID)

app/views

Your view files.
The structure is made so that having a controller named TestController, it looks
in the app/views/test/ folder for it’s view files.

  • All view files end with .phtml

Having an action in the TestController called index, the view file
app/views/test/index.phtml will be rendered as default.

config/routes.php

Your routes around the system needs to be defined here.
A route consists of the URL you want to call + the controller#action you want it
to hit.

An example is:
$routes = array(
‘/test’ => ‘test#index’ // this will hit the TestController’s indexAction method.
);

Error handling

A general error handling has been added.

  • If a route doesn’t exist, then the error controller is hit.
  • If some other exception was thrown, the error controller is hit.

As default, the error controller just shows the exception occured, so remember
to style the error controller’s view file (app/views/error/error.phtml)

Changelog

I’ll start having changelog information at the bottom of this page.

Version 0.6

  • Completely revised system with controllers, models and views.
  • New router added.
  • You now have to define routes
  • added a config/setting.ini file, where you define database settings (password etc)

Version 0.3.3

  • Added the missing vendor folder.
  • Fixed an error with exposing the application root (.htaccess file fix).
  • Fixed an error where all files in the vendor scripts folder was read into the system. Only .php files are now read.