kevinkaske / pooch
A simple lightweight MVC PHP framework
Requires
- php: >=8.1
- kevinkaske/flash: dev-master
- robmorgan/phinx: *
- thingengineer/mysqli-database-class: dev-master
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-04 07:17:37 UTC
README
A simple, lightweight MVC framework for PHP.
PoochHQ.com
Pooch
A simple lightweight PHP framework. I created Pooch while building Where I Give, an application to make it easy for non-profit organizations to receive donations online.
Table of Contents
- About
- Requirements
- Installation
- Project Structure
- Getting Started
- Configuration
- Routing
- Controllers
- Views and Layouts
- JSON and XML Responses
- Assets (CSS, JavaScript and Images)
- Scheduled Jobs
- Error Pages
- Debugging
- In Memory of Emma
- License
About
Pooch is a simple, small PHP framework. Pooch is able to stay tiny by being built to use the amazing MysqliDb database class and built in PHP functionality (for templating). This framework focuses on convention over configuration. It abuses global variables as a necessary evil to keep code as light and simple as possible. It works great for prototyping small to medium sized web applications and getting your idea into a functioning product.
Who should use this framework? Anyone that wants to get an application up and running quickly and focus on their code and not the framework. If you want a small, light, quick framework and don't mind being tied to MySQL, this framework could be what you have been looking for.
Pooch is built on a handful of small, well-known pieces:
| Package | Purpose |
|---|---|
| thingengineer/mysqli-database-class | Database access (available as $this->db in every controller) |
| robmorgan/phinx | Database migrations |
| kevinkaske/flash | Flash messages that survive a redirect |
Requirements
- PHP 8.1 or newer
- MySQL (or a MySQL-compatible database such as MariaDB)
- Composer
- A web server that can rewrite URLs to
index.php(Apache withmod_rewrite, Nginx, or PHP's built in server for development)
Installation
Quick Install (macOS or Linux)
Run this command from the folder you want your new application to live in:
curl -fsSL https://raw.githubusercontent.com/kevinkaske/pooch/master/setup/install.sh | bash
It asks what to name your application's folder, creates the folder, downloads Pooch with Composer and sets up a skeleton application inside it. To skip the question, pass the folder name at the end:
curl -fsSL https://raw.githubusercontent.com/kevinkaske/pooch/master/setup/install.sh | bash -s -- pound
When it finishes, start your application:
cd pound
php -S localhost:8000 router.php
The installer checks that PHP 8.1 or newer, Composer and curl are installed, and stops without changing anything if a folder with that name already exists. You can read the script before running it.
Manual Installation
Installation of pooch is through composer. You simply need to create a directory and create a composer.json file with the following contents:
{
"minimum-stability": "dev",
"require": {
"kevinkaske/pooch": "dev-master"
}
}
Alternatively you can simply download the contents of this file to your application dir.
Then run composer install. Composer will then install the dependent libraries.
Now we need to setup the project. Run the following command php vendor/kevinkaske/pooch/setup/new_application.php. This copies a
skeleton application into your project directory. Congratulations... Your project is now ready to go.
Welcome to pooch!
Manual Installation Example (macOS or Linux)
The following commands will create a pooch application for an application named "pound". This is what the quick install script does for you.
mkdir pound
cd pound
curl -fLO https://raw.githubusercontent.com/kevinkaske/pooch/master/setup/base/composer.json
composer install
php vendor/kevinkaske/pooch/setup/new_application.php
Project Structure
The setup script creates the following structure in your application directory:
pound/
├── .htaccess # Apache rewrite rules (routes everything through index.php)
├── index.php # Application entry point
├── router.php # Router for PHP's built in web server
├── phinx.yml # Database connection details for migrations
├── composer.json
├── assets/
│ ├── css.php # List of CSS files to load
│ ├── js.php # List of JavaScript files to load
│ ├── css/style.css
│ ├── js/scripts.js
│ └── img/ # Application images
├── config/
│ ├── config.php # Application configuration
│ └── routes.php # Custom route exceptions
├── controllers/
│ ├── application_controller.php # Base controller for your app
│ └── welcome_controller.php # Default root controller
├── jobs/ # Scheduled tasks (called from cron)
├── lib/
│ └── autoload.php # Include libraries not managed by composer
├── migrations/ # Phinx migration files
└── views/
├── layouts/application.php # Default layout
└── welcome/index.php # View for WelcomeController::index
Getting Started
Servers
Built in PHP Server
A router.php file is included to let you serve your application from PHPs built in server. Simply run the following command from the application's root directory:
php -S localhost:8000 router.php
New applications are already set up for this: in development, $config['address'] defaults to http://localhost:8000 and
$config['controller_indent'] to false. If you serve the app from a different address or port, change these in
config/config.php (see Configuration).
Apache
A .htaccess file is included by default to route requests through index.php and remove index.php from the URL.
Nginx and Other Servers
If you are using a webserver besides Apache you will need to configure it to rewrite the URL and pass the request through index.php. For Nginx, something like the following works:
location / { try_files $uri $uri/ /index.php?$query_string; }
Database Setup
We are now ready to hook the database up. You can either use a GUI tool like MySQL Workbench or the MySQL CLI. To create a DB named "pound" using the CLI type the following:
mysql -u root -p
CREATE DATABASE pound;
You will then need to update the config files to point at this new database. Update the database connection details in both config/config.php and phinx.yml files.
Database Migrations
Database migrations give you a way of keeping track of changes to your database schema in source control. This way you can keep your various database environments in sync.
Let's create a table for our new dog pound app called "dogs". We use the following command to create a migration:
php vendor/bin/phinx create CreateDogs
This will create a file in the migrations folder with a time stamp and the name CreateDogs. This will look something like 20151113201209_create_dogs.php. You will then need to edit the contents of the file. You can find more information on how the Phinx migration files work here. For now we will just edit the file to look like the following:
<?php use Phinx\Migration\AbstractMigration; class CreateDogs extends AbstractMigration{ public function change(){ $dogs = $this->table('dogs'); $dogs->addColumn('name', 'string') ->addColumn('age', 'string') ->save(); } } ?>
Now let's run this migration and add the "dogs" table to the database. Run php vendor/bin/phinx migrate from the command line to run
this migration.
Controllers and Views
We now have a place to store the dogs that are in the dog pound. How are we going to allow people to view, add, and update these dogs? We need to create a dogs controller and views to go along with it.
Create a blank text document at controllers/dogs_controller.php. In this file put the following code:
<?php class DogsController extends ApplicationController{ function index(){ //Create an array to hold values to pass to view $values = array(); //Get all of the dogs in DB $values['dogs'] = $this->db->get("dogs"); //Send array to view $this->values = $values; } } ?>
Create a blank text document at views/dogs/index.php with the following code:
<?php foreach ($view_data['dogs'] as $dog) { echo $dog['name'].'<br>'; } ?>
Now manually adding a new record (through a tool like Sequel Pro or MySQL Workbench) to the database table will make it appear when you navigate to http://localhost:8000/dogs.
Configuration
All application configuration lives in config/config.php in the global $config array.
| Option | Description |
|---|---|
$config['env'] |
'dev' or 'prod'. Controls asset handling, job key checks and which address block is used. |
$config['address'] |
The base URL of your application (e.g. http://localhost:8000). Used to build links, asset URLs and redirects. Set separately in the dev and prod blocks. |
$config['controller_indent'] |
Use when your app does not live at the root of the server (e.g. http://localhost/example). Set it to the subdirectory name as a string ('example', or 'apps/example' for several levels), or to true together with $config['controller_indent_directory']. Set to false when the app is at the server root. |
$config['controller_indent_directory'] |
The subdirectory name used when controller_indent is true. |
$config['root_controller'] / $config['root_action'] |
The controller and action called for /. Defaults to welcome / index. |
$config['404_page'] |
Path (relative to the app root) of the page shown when a controller or action can't be found. |
$config['job_key'] |
Secret key required to run scheduled jobs in production. Change this. |
$config['password_options'] |
Options you can pass to PHP's password_hash(), e.g. ['cost' => 6]. |
$config['db_host'], db_username, db_password, db_database |
MySQL connection details. |
The config file also sets the timezone, locale and PHP error reporting. You are free to add your own application specific
options under the "Custom Application Specific Options" section; $config is available in every controller ($this->config) and view.
If you need to load libraries that aren't managed by composer, include them from lib/autoload.php.
Routing
Conventional Routes
How does the application know what controller and view to call? The application takes apart the URL and uses its components to determine the Controller, Action and an optional ID:
http://localhost/dogs/show/12
│ │ └─ $this->id = '12'
│ └────── action = show()
└─────────── controller = DogsController (controllers/dogs_controller.php)
- If there is no action passed in via the URL, it defaults to
index. Example: http://localhost/dogs would call the index function in the dogs controller. - Requesting
/calls theroot_controllerandroot_actionfrom the config. - Dashes in the controller segment are converted to underscores, so
/dog-walkersloadscontrollers/dog_walkers_controller.phpand theDogWalkersControllerclass. - If the controller file or action doesn't exist, a 404 is returned.
Is this limiting? Yes. It does, however, make it very fast and easy to keep track of your routes. You will always know what controller and action is being called.
Custom Routes
For the times when a URL needs to break the convention, add an exception to config/routes.php. Each route is an array of
path, controller, action:
<?php $config['routes'] = array( array('/login', 'sessions', 'login'), array('/logout', 'sessions', 'logout') ); ?>
Custom routes are matched exactly against the URL path (trailing slashes are ignored). When controller_indent is set, the indent
directory is automatically prepended, so you don't need to include it in your routes.
Query Strings
Query string values are parsed into the $query_string array (available as $this->query_string in controllers and $query_string in
views):
// http://localhost/dogs?breed=pitbull $breed = $this->query_string['breed'];
Note: if the same key appears more than once (?id=2&id=3), only the last value is kept.
Controllers
Controllers live in controllers/ and are named {name}_controller.php with a class named {Name}Controller that extends
ApplicationController. Each public function is an action.
ApplicationController
controllers/application_controller.php is the base class for all of your controllers. Put code that should run on every request
(authentication checks, loading the current user, etc.) in its constructor.
What's Available in a Controller
| Property / Method | Description |
|---|---|
$this->db |
A connected MysqliDb instance. |
$this->values |
Array of data to send to the view (available there as $view_data). |
$this->layout |
Layout to render with. Defaults to 'application'. Set to 'none' to render the view without a layout. |
$this->config |
The $config array. |
$this->controller, $this->action, $this->id |
The current controller name, action name and ID from the URL. |
$this->query_string |
Parsed query string values. |
$this->response_type |
'html', 'json' or 'xml' (see JSON and XML Responses). |
$this->redirect($url) |
Sends a redirect and stops execution. Flash messages are kept for the next request. |
$this->addToApplicationData($key, $value) |
Adds data to $application_data, which is available in layouts, views and partials. Handy for things like the current user's name in a navigation bar. |
$this->sendMessageToConsole($var) |
var_dumps a value to the PHP built in server's console. See Debugging. |
Example
<?php class DogsController extends ApplicationController{ function show(){ $this->db->where('id', $this->id); $this->values['dog'] = $this->db->getOne('dogs'); } function create(){ $this->db->insert('dogs', array( 'name' => $_POST['name'], 'age' => $_POST['age'] )); $this->redirect('/dogs'); } } ?>
The view is rendered automatically once the action finishes, so there's no need to call a render function.
Views and Layouts
Once the correct action/function has been called everything will be displayed using first the layout and then the view.
- The layout defaults to
views/layouts/application.php. Change it per action with$this->layout = 'admin';to useviews/layouts/admin.php, or$this->layout = 'none';to skip the layout entirely. - The layout decides where the view goes by calling
renderView(). - The view defaults to
views/{controller}/{action}.php. Example: http://localhost/dogs/show would use theviews/dogs/show.phpview file.
Views are plain PHP files. The following variables are available inside layouts, views and partials:
| Variable | Description |
|---|---|
$view_data |
The array set in $this->values in the controller. |
$application_data |
Data added with addToApplicationData(). |
$config |
The application configuration. |
$query_string |
Parsed query string values. |
$controller, $action |
The current controller and action names. |
RESTful routes are a good way to keep your application organized. Pooch, however, does not force your application to be RESTful. If you would like to call an action function display instead of show, go for it.
Partials
Reuse snippets of markup with includePartial(). The path is relative to the views directory:
<?php includePartial('dogs/_dog_card'); ?> // includes views/dogs/_dog_card.php
View Helpers
| Helper | Description |
|---|---|
renderView() |
Renders the current action's view (call this from your layout). |
includePartial($path) |
Includes views/{$path}.php. |
getAddress() |
Returns the application's base address, including the controller indent directory if one is set. Use it to build links: <a href="<?php echo getAddress(); ?>/dogs">. |
getPath($path) |
Returns $config['address'] joined with $path. |
getImage($name) |
Returns the URL for an image in assets/img/. In production a deploy ID is appended for cache busting. |
cssTags() / jsTags() |
Output the <link> and <script> tags for your assets. |
getValueIfExists($val) |
Returns the value, or an empty string if it isn't set. |
JSON and XML Responses
Pooch can respond with HTML, JSON or XML from the same action. The response type is chosen from (in order):
- A
response_typePOST parameter (html,jsonorxml) - A
response_typequery string parameter, e.g./dogs?response_type=json - The request's
Acceptheader (text/html,application/jsonorapplication/xml)
Defaults to html. For JSON and XML, Pooch sets the correct Content-Type header, skips the layout and renders
views/{controller}/{action}.{type}.php instead of the normal view:
<?php // views/dogs/index.json.php echo json_encode($view_data['dogs']); ?>
This makes it easy to build an API alongside your HTML pages without writing a second set of controllers.
Assets (CSS, JavaScript and Images)
List your stylesheets and scripts (without extensions) in assets/css.php and assets/js.php. Files live in assets/css/ and
assets/js/:
<?php // assets/css.php $css_files = array( 'reset', 'style' ); ?>
Then output them in your layout:
<?php echo cssTags(); ?> <?php echo jsTags(); ?>
In development each file gets its own tag so you can debug them individually. In production, cssTags() and jsTags() output a
single combined file (assets/css/master{$css_md5}.css and assets/js/master{$js_md5}.js). Your deploy process is responsible for
building these combined files and for creating a deploy_id.php file in the app root that sets $deploy_id, $css_md5 and
$js_md5. cssTagsForDeploy() and jsTagsForDeploy() print the list of files in load order to help a build script concatenate them.
Images go in assets/img/ and are referenced with getImage('logo.png').
Scheduled Jobs
Jobs are tasks meant to be run on a schedule (for example, from cron). Put them in the jobs/ directory. A job named
send_reminders lives at jobs/send_reminders.php and defines a SendRemindersJob class with an index() function:
<?php class SendRemindersJob extends ApplicationController{ function index(){ // do the work here } } ?>
Jobs are run by requesting /jobs/{job_name}. In production a key query parameter matching $config['job_key'] is required:
# crontab: run every night at 2am 0 2 * * * curl -s "https://www.example.com/jobs/send_reminders?key=YOUR_JOB_KEY" > /dev/null
Error Pages
When a controller or action can't be found, Pooch responds with a 404 status and the page set in $config['404_page']. A default
page ships with the framework; point the config option at your own file to customize it.
Debugging
When using PHP's built in server, $this->sendMessageToConsole($anything) will var_dump a value into the terminal running the server
instead of the browser. This is great for inspecting data without breaking your page's output.
In development, config/config.php turns on full error reporting and displays errors on the page.
More to Come
This is just an overview of the inner workings of the Pooch framework. We are working on more detailed documentation.
In Memory of Emma
The dog in the Pooch logo is Emma. She was the goodest dog, and passed away in 2024. Pooch is dedicated to her memory.
License
Pooch is open source software licensed under the MIT license.

