| Recommend this page to a friend! | 
|  Download | 
| Info | Documentation |  Files |  Install with Composer |  Download | Reputation | Support forum | Blog | Links | 
| Ratings | Unique User Downloads | Download Rankings | ||||
| Not yet rated by the users | Total: 125 | All time:  9,448 This week: 455  | ||||
| Version | License | PHP version | Categories | |||
| dframe-router 4.1.7 | The PHP License | 7 | PHP 5, Code Generation, Configuration, P... | 

$ composer require dframe/router
Creating an application, it's worth taking care of their friendly links. Its has a big part in position in SEO. Link router work in a similar way as network router. It is responsible for calling the method from controller.
$this->router->addRoute([
'page/:method' => [
'page/[method]/',
'task=page&action=[method]'
]
]);
$this->router->makeUrl('page/:action?action=index'); // Return: https://example.php/page/index
$this->router->isActive('page/:action?action=index'); // Current Website true/false
We define the table with adresses for our application in the configuration file
'_params' => [
'[name]/[value]/',
'[name]=[value]'
]
defines the way the additional foo=bar parameters should be interpreted.
Config/router.php
 
 return [
     'https' => false,
     'NAME_CONTROLLER' => 'page',    // Default Controller for router
     'NAME_METHOD' => 'index',       // Default Action for router
     'publicWeb' => '',              // Path for public web (web or public_html)
 
     'assets' => [
         'minifyCssEnabled' => true,
         'minifyJsEnabled' => true,
         'assetsDir' => 'assets',
         'assetsPath' => APP_DIR.'View/',
         'cacheDir' => 'cache',
         'cachePath' => APP_DIR.'../web/',
         'cacheUrl' => HTTP_HOST.'/',
     ],
 
     'routes' => [
         'docs/:pageId' => [
             'docs/[pageId]/', 
             'task=page&action=[docsId]&type=docs'
         ],
         
         'error/:code' => [
             'error/[code]/', 
             'task=page&action=error&type=[code]',
             'code' => '([0-9]+)',
             'args' => [
                 'code' => '[code]'
             ],
         ],
         
        ':task/:action' => [
            '[task]/[action]/[params]',
            'task=[task]&action=[action]',
            'params' => '(.*)',
            '_params' => [
                '[name]/[value]/',
                '[name]=[value]'
            ]
        ],
         'default' => [
             '[task]/[action]/[params]',
             'task=[task]&action=[action]',
             'params' => '(.*)',
             '_params' => [
                 '[name]/[value]/', 
                 '[name]=[value]'
             ]
         ]
     ] 
 
 ];
- makeUrl - is used for generating the full adress. For example |makeurl| - method used for redirections, equivalent of |header| but with a parameter being a key from the Config/router.php table. In case of using docs/:docsld it looks as the following |redirect|
Controller/Page.php
 namespace Controller;
 
 use Dframe\Controller;
 use Dframe\Router\Response;
 
 class PageController extends Controller
 {
 
     /
      * @return bool
      */
     public function index()
     {
         echo $this->router->makeUrl('docs/:docsId?docsId=23');
         return;
     }
 
     /
      * @return mixed
      */
     public function docs()
     {
 
         if (!isset($_GET['docsId'])) {
             return $this->router->redirect('error/:code?code=404');
         }
     }
 
     /
      * @param string $status
      *
      * @return mixed
      */
     public function error($status = '404')
     {
         $routerCodes = $this->router->response();
 
         if (!array_key_exists($status, $routerCodes::$code)) {
             return $this->router->redirect('error/:code?code=500');
         }
 
         $view = $this->loadView('index');
         $smartyConfig = Config::load('view/smarty');
 
         $patchController = $smartyConfig->get('setTemplateDir', APP_DIR . 'View/templates') . '/errors/' . htmlspecialchars($status) . $smartyConfig->get('fileExtension', '.html.php');
 
         if (!file_exists($patchController)) {
             return $this->router->redirect('error/:code?code=404');
         }
 
         $view->assign('error', $routerCodes::$code[$status]);
         return Response::create($view->fetch('errors/' . htmlspecialchars($status)))->headers(['refresh' => '4;' . $this->router->makeUrl(':task/:action?task=page&action=index')]);
     }
 
 }
assign - it's a method of the template engine that assignes value to a variable which is used in the template files.
View/templates/index.html.php
https://
Using only PHP
View/index.php
namespace View;
use Dframe\Asset\Assetic;
class IndexView extends \View\View
{
     /
      * @return bool
      */
     public function init()
     {
         $this->router->assetic = new Assetic();
         $this->assign('router', $this->router);
     }
}
Extention of the basic Dframe\Router is Dframe\Router\Response, adding functionality of setting the response status (404, 500, etc.) and their headers.
return Response::create('Hello Word!')
    ->status(200)
    ->headers([
                  'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
                  'Cache-Control' => 'no-cache',
                  'Pragma',
                  'no-cache'
              ]);
For generating html.
Render json
return Response::renderJSON(['code' => 200, 'data' => []]);
Render json with callback
return Response::renderJSONP(['code' => 200, 'data' => []]);
Redirect
return Response::redirect(':task/:action?task=page&action=login');
|  Files (10) | 
| File | Role | Description | ||
|---|---|---|---|---|
|  Exceptions (3 files) | ||||
|  Tests (2 files) | ||||
|    composer.json | Data | Auxiliary data | ||
|    README.md | Doc. | Documentation | ||
|  Request.php | Class | Class source | ||
|  Response.php | Class | Class source | ||
|  Router.php | Class | Class source | ||
|  Files (10) | / | Exceptions | 
| File | Role | Description | 
|---|---|---|
|  InvalidArgumentException.php | Class | Class source | 
|  RouterException.php | Class | Class source | 
|  RuntimeException.php | Class | Class source | 
|  Files (10) | / | Tests | 
| File | Role | Description | 
|---|---|---|
|  ResponseTest.php | Class | Class source | 
|  RouterTest.php | Class | Class source | 
| The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. | 
|  Install with Composer | 
| Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
| 100% | 
 | 
 | 
| Applications that use this package | 
 If you know an application of this package, send a message to the author to add a link here.
 If you know an application of this package, send a message to the author to add a link here.