Front Controller

Your web/index.php serves as the front controller and contains the following content:

<?php# File: web/index.phpnamespaceExample\Project; // Change this to whatever base namespace you are using in your project.$app_dir=dirname(__DIR__);// Use Composer's autoloader.require_once$app_dir.'/vendor/autoload.php';// Pass a couple options to our dispatcher.$opts= ['app_dir'=>$app_dir, 'namespace'=>__NAMESPACE__];$app=new\werx\Core\Dispatcher($opts);// Dispatch the request.$app->dispatch();

This will turn control of dispatching the request to the werx.Core dispatcher.

Basic Configuration Usage

Multiple Environment Configuration Support

The configuration manager supports different configs for different environments (local, dev, test, prod, etc).
To activate per-environment configs, create a sub-directory of src/config named the same as your environment.
This directory should contain configuration files with any items from the main config you want to override in that environment.

Example:

src/config/test/database.php

You can tell the app which environment you are running in by modifying the content of src/config/environment to contain the name of your active environment.

Then load your config as you normally would. The configuration items will be merged between the default config environment-specific overrides.

Templates

Basic Template Usage

classHomeextendsController{publicfunction__construct() {// Set our default template.$this->template->layout('layouts/default'); }publicfunctionindex() {// Set some variables for all views.$this->template->page_title='Werx Skeleton';// Render and Display the home/index view, passing a variable named "heading".$this->template->output('home/index', ['heading'=>'Congratulations, it worked!']);// Same as above, but return the rendered content instead of displaying.// $content = $this->template->render('home/index', ['heading' => 'Congratulations, it worked!']); }}

I've extended the standard Template class from Plates in 2 important ways:

I've added the output() method so you can display the rendered view without an echo if you like.

All variables are escaped before rendering/displaying the content to prevent cross site scripting.

If there are variables you don't want auto-escaped (example: pre-rendered HTML), you can prevent escaping by calling unguard('param_name').

Prefill

I've added some functionality to the Template class for easily getting default values in views when the variable doesn't exist.

classHomeextendsController{publicfunction__construct() {// Set our default template.$this->template->layout('layouts/default'); }publicfunctionindex() {// Grab our prefill content from the request and mass assign.$this->template->setPrefill($this->input->get());// Or we can pull the prefill content from session.$this->template->setPrefill($this->session->get('prefill'));// Render and Display the home/index view.$this->template->output('home/index'); }}

Then in our view:

<!-- Prefill from the 'foo' variable if it exists. If not, it will default to null. -->
<input type="text" name="foo" value="<?=$this->prefill('foo')?>" />
<!-- Prefill from the 'bar' variable if it exists. If not, it will default to 'some default value'. -->
<input type="text" name="bar" value="<?=$this->prefill('bar', 'some default value')?>" />

$_GET and $_POST

The Core Controller has a reference to the httpFoundation Request object, but I find the syntax for accessing $_GET and $_POST attributes less than ideal. So, I've built a wrapper around the httpFoundation Request object to make the syntax friendlier.

Examples:

# Symfony way to access an attribute from $_POST$foo=$this->request->request->get('foo');# Werx way to access an attribute from $_POST$foo=$this->input->post('foo');# Symfony way to access an attribute from $_GET$foo=$this->request->query->get('foo');# Werx way to access an attribute from $_GET$foo=$this->input->get('foo');# Symfony way to access the entire $_POST array$post=$this->request->request->all();# Werx way to access the entire $_POST array$post=$this->input->post();# Symfony way to access the entire $_GET array$get=$this->request->query->all();# Werx way to access the entire $_GET array$get=$this->input->get();

null is returned when trying to access a non-existent attribute from get/post. Pass a 2nd parameter to $this->input->get() or $this->input->post() to serve as the default value if you want something other than null.

// Returns 'foo'$foo=$this->input->post('nonexistent', 'foo');

In the default Request object, you have to pass true as the 3rd argument to $this->request->query->get() in order to access "deep" array keys. This is defaulted to true in my override.

$bar=$this->input->get('foo[bar]');

See the Symfony Docs for more
information on the HttpFoundation component.