1. Go to this page and download the library: Download talesoft/tale-controller library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
talesoft / tale-controller example snippets
use Tale\App;
use Tale\Controller;
//Define a controller of some kind
class MyController extends Controller
{
//GET|POST /
public function indexAction()
{
$res = $this->getResponse();
$res->getBody()->write('Hello index!');
return $res;
}
//GET|POST /?action=about-us
public function aboutUsAction()
{
$res = $this->getResponse();
$res->getBody()->write('About us!');
return $res;
}
//GET /?action=contact
public function getContactAction()
{
$res = $this->getResponse();
$res->getBody()->write('Contact form!');
return $res;
}
//POST /?action=contact
public function postContactAction()
{
//Handle contact form
$res = $this->getResponse();
$res->getBody()->write('Success!');
return $res;
}
}
//Create a new app context
$app = new App();
//Make sure we can target the "action" somehow.
//Normally you'd use a router, we use a simple GET-variable in this case
//"index.php?action=about-us" would dispatch "MyController->aboutUsAction"
//This is a simple middleware mapping query's "action" to the
use Tale\App;
use Tale\Controller\Dispatcher;
//Create a new app context
$app = new App([
'controller' => [
'nameSpace' => 'My\\Controllers',
'loader' => ['path' => __DIR__.'/app/controllers']
]
]);
//This is a middleware mapping "module", "controller" and "action" GET-values to
//ServerRequestInterface-attributes
$app->append(function($req, $res, $next) {
$params = $req->getQueryParams();
$module = isset($params['module']) ? $params['module'] : null;
$controller = isset($params['controller']) ? $params['controller'] : null;
$action = isset($params['action']) ? $params['action'] : null;
if ($module)
$req = $req->withAttribute('module', $module);
if ($controller)
$req = $req->withAttribute('controller', $controller);
if ($action)
$req = $req->withAttribute('action', $action);
return $next($req, $res);
});
//Append our dispatcher middleware
$app->append(Dispatcher::class);
//Display the app
$app->display();
class FirstDispatcher
{
public function getOptionNameSpace()
{
return 'firstDispatcher';
}
}
class SecondDispatcher
{
public function getOptionNameSpace()
{
return 'secondDispatcher';
}
}
$app->get(Router::class)
->all('/:controller?/:action?', FirstDispatcher::class)
->all('/sub-module/:controller?/:action?', SecondDispatcher::class);
$app->display();