5. Rename config.dist.php to config.php and edit the constants *HTTP_URL* and *DIR_APPLICATION* in file **config.php**. Edit others _DIR\_\*_ constants path if necessary.
All routes are a mapping class with extends the primary Controller class. In a simple term, the class name in controller is *Controller*___Folder___*Filename*.
In the last we finish with a simple `$this->out();` to indicate the output and process template file. It's an automated mechanism to link this controller with the respective viewer.
The viewer in this sample is a twig template format ([Twig page](https://twig.symfony.com)). ExacTI Phacil Framework permits to use various PHP template engines, like Twig, Mustache, Dwoo and Smarty.
When you call a `$this->response->setOutput` inside a controller, you specify an output of content to screen without template. It's very useful for JSON, XML or others data contents.
The `$this->out` is a smart way to output content to screen and combine the render with an associative template. For sample, if you have a controller in a file called `sample.php` inside the `demo` controller folder and a viewer called `sample.twig` inside `demo` view folder, automatically links with one another and the `$this->template` isn't need (Unless you want to specify another template with a different name).
If you specify `$this->out(false)` the auto childrens "header" and "footer" are not loaded.
For functions inside the controller that are different from index, for automatic mapping with the template it is necessary to add an underscore (_) after the controller name and add the function name (E.g.: contact_work.twig to link with the route common/contact/work, see *Routes* section for more information).
##### Sample
```php
<?php
class ControllerCommonContact extends Controller {
public function index() {
$this->document->setTitle('Contact Us');
$this->out();
// automatically link with common/contact.twig template
}
public function work() {
$this->document->setTitle('Work with Us');
$this->out();
// automatically link with common/contact_work.twig template
To use a determined template engine, just create a file with name of engine in extension, for sample, if you like to use a Twig, the template file is **demo.twig**, if desire the Mustache template, use **demo.mustache** extension file.
The ExacTI Phacil Framework allows to use various template engines in the same project.
This framework contains the PhpFastCache (https://www.phpfastcache.com) library to provide a most efficient cache system with many possibilities, like Mencache, Redis, APC or a simple file cache.
To enable this library, define USE_PHPFASTCACHE constant as true. It works only in PHP 7 or newer.
If you don't set USE_PHPFASTCACHE or use an older version of PHP, Phacil automatic set to simple internal file cache system.
To storage caches in binary mode (most fast), PHP need the igbinary extension (https://github.com/igbinary/igbinary). Without igbinary the framework works fine, but cached values is stored using serialize PHP function.
You can configure to use automatic cache in SQL databases with SQL_CACHE constant configuration (see in Config section) also you can use for manual caches using in a controller or model with `$this->cache->set($key, $value)` to set a cache and `$this->cache->get($key)` to obtain a cache value.
If you need an extra database connection or a different driver/database access, you can use the `$this->load->database($driver, $hostname, $username, $password, $database);` method (see more about databases functions in the Databases section of this document).
The name of database is a registered object to access database functions.
This load is simple and registry to object of origin.
This framework is totally MVC (Model, View and Controller) based. The models are just like the controllers and uses the same structure, with a different folder.
To create a model, put in the models folder a directory and file with the code.
```php
<?php
class ModelFolderFile extends Model {
public function SayMyName() {
return "Heisenberg";
}
}
```
To use this model in a controller, you can use the `loader`.
In some cases, we need to add a __construct or __destruct in a class to better code practices. To call a constructs in controllers and models, use the `$registry` parent:
To use a magic request system, you just need to call a `$this->request` method. For sample, to obtain a POST value, use `$this->request->post['field']` to get the post value with security.
For REST services you can use `$this->request->method` to check if is a POST, GET, PUT, DELETE or other HTTP method. You can use the auxiliary functions to determinate the HTTP method, like `$this->request->isPUT()` to return *true* or *false* for PUT method. Also others methods have your functions, like `$this->request->isPOST()`, `$this->request->isGET()`, `$this->request->isHEAD()`, `$this->request->isCONNECT()`, `$this->request->isOPTIONS()`, `$this->request->isTRACE()`, `$this->request->isPATCH()` or `$this->request->isDELETE()`. All are equivalent to "is" function in request, like this sample: `$this->request->is('PUT')` return true or false for PUT HTTP method.
All sessions called with Phacil are secure for HTTPS and IP verify, in other words, we made the session cookie stolen more difficulty, to have less chance of session hijacking.
You can define the session prefix name with constant `SESSION_PREFIX` in your config file. For default, the prefix is "SESS".
| `$this->children` | array | Load in a template variable other renders of web app. Th children's *footer* and *header* is default loaded when uses `$this->out()` output. To load without this defaults childrens, use `$this->out(false)`.|
| `$this->redirect($url)` | string | Create a header to redirect to another page. |
## Routes
The ExacTI Phacil Framework is a simple router base to create a simple and consistent web navigation.
Without SEO URL, we invoke a page with *route* get parameter when contains a scheme folder/file of controller, like this: *http://example.com/index.php?route=folder/file*.
In a sample case, we have this controller:
```php
<?php
class ControllerFolderFile extends Controller {
public function index() {
echo "Index";
}
public function another() {
echo $this->foo($this->request->get['p']);
}
private function foo($parameter) {
return ($parameter != 0) ? "another" : "other";
}
}
```
If we access *index.php?route=folder/file* we see the "Index" message. But, like a tree, if we access *index.php?route=folder/file/another* obtains another function code, with "other" message.
Multiple Get parameters is simple and we can define normally, e.g.: *index.php?route=folder/file/another&p=2* to print "another" message.
Private and protected functions is only for internal use, if we tried to access *index.php?route=folder/file/foo*, the framework return 404 HTTP error.
If you need a beautiful URL for SEO or other optimizations, you can use the url_alias database table. Is very simple and "translate" the routes in URL.
Execute this SQL in your database to create a url_alias table:
With the url_alias, the access to route *http://example.com/index.php?route=contact/contato* and *http://example.com/contact* is the same!(and very pretty!).
You can create URL without a SQL Database configuration. For this you just need specify routes in the config file using the ***ROUTES*** constant with array content, like this:
We have a function to create a strict and dynamic links automatic: `$this->url->link($route, $args = '', $connection = 'NONSSL')`. Is a simple function to generate internal links with correct URL encode.
In sample of this table above, if we have this code:
```php
echo $this->url->link('contact/contato');
```
We get this URL in output: *http://example.com/contact*
But if you don't have a route in url_alias table, returns the complete route.
```php
echo $this->url->link('catalog/products');
```
Return this URL in output: *http://example.com/index.php?route=catalog/products*
***Note:*** *It's necessary specify the config `config_seo_url` for correctly function of this URLs. If you use the SQL url_alias table, you need specify the `USE_DB_CONFIG` to true in config file.*
Use the *'__%__'* character and its variations (see below) to create a route with wildcard. All contents in the URL will matches with wildcard is passed to your controller function as argument.
In this sample above, imagine the URL _http://yoursite.com/produto/**87**/**alfajor**/promo_, this URL is sending to function *presentes* values for `$id` and `$name` arguments in sequential method, in other words, the value of `$id` is set to `87` and `$name` to `alfajor`. The wildcard *%d* in router relative to `$id` argument define is only accepted number values.
The `$this->response->isJSON` is responsible to send a JSON Content-Type HTTP header to browser. The `$this->response->setOutput()` works for send output of the content of a variable, function, constant, etc., to the browser.
In response we have *setOutput* function with parameters to facility for JSON and REST use.
The parameters are `$this->response->setOutput($output, bool $isJSON, int $HTTPCODE, string $HTTPDESC);` on automatic apply the JSON Content-type header if the second parameter is true and define the HTTP response code in third parameter. A description for HTTP code also can be send, if you need.
Except for the first parameter, the others are optional.
#### Sample of use for JSON Rest output only with setOutput function
The config library in Phacil Framework obtains values to use in entire web app of: `$config` array in *config.php* file, the settings table in your configured database or both.
Is based in key->value mode with options like serialized value.
To use a configuration value, just call `$this->config->get('nameOfKey')`.
To storage a config value, use a SQL table called settings or an array.
If you need to register a class or other resource to use in your entire application, you can create using the file *registrations.php* in the **system** folder.
Use to specify a `$this->registry->set('name', $function);` with the function, class or method if you need. After this, just use `$this->name` to access this registry inside a controller or model.
***Pay attention:*** *Use the registrations only for objects that you need to access from the entire application. E.g.: If you need to connect a one more database but you just use for one model, it's better to load just inside the model, for awesome performance.*
To add new classes or functions, you can put a folder in system directory with a autoload.php file. All autoload.php files are included to framework automatically.
Composer is an application-level package manager for the PHP programming language that provides a standard format for managing dependencies of PHP software and required libraries.
To use Composer packages and autoloads, just configure your _composer.json_ with this vendor-dir: system/vendor.