Giter Site home page Giter Site logo

router's Introduction

Extra\Routing

Adding route

Route takes 2 parameters.
The first is the URL pattern. The second is the 'action' (to be performed)

use Extra\Routing\Route;

Route::get('/', function(){
    return 'Hello World';
});

Available methods for adding routes

use Extra\Routing\Route;

Route::any('/', 'action');
Route::get('/news', 'action');
Route::post('/news/{id}', 'action');
Route::put('/news/{id}', 'action');
Route::delete('/news/{id}', 'action');
Route::some(['head', 'options', 'connect', 'trace', 'patch'], '/test', 'action');

Available 'actions' for route

use Extra\Routing\Route;

// Function name
Route::get('/', 'FunctionName');

// The class name and method name are specified through the @ separator
Route::get('/', 'ClassName@method');

// Callback function
Route::get('/', function(){
    return 'Hello World';
});

Search and execute matching route

use Extra\Routing\Router;

try {
    $router = Router::getInstance()->includeRoutesFile('path_to_file_with_routes.php');

    $route = $router->match($requestUri, $requestMethod);
    $result = $route->execute($requestUri);
}
catch (\Exception $e){
   die($e->getMessage());
}

Execute route

In order for the parameter to be passed to the function,
it is necessary that the name of the variable that the function accepts
has the same name as indicated in the URL {id}

use Extra\Routing\Router; 

$route = Route::get('/news/{id}', function($id){
    return 'Id of the news: ' . $id;
});

$requestUrl = '/post/86';
$result = $route->execute($requestUrl);

echo $result; // 'Id of post: 86'

Setting route name

use Extra\Routing\Route;

Route::get('/news/', 'NewsController@list')->name('news.list');
Route::get('/news/{id}', 'NewsController@detail')->name('news.detail');

Setting route rules

use Extra\Routing\Route;

// One rule
Route::delete('/news/{id}', 'NewsController@delete')->rule('id', '[0-9]+');

// Few rules
Route::post('/catalog/{section}/{id}', 'CatalogController@detail')->rules([
    'section' => '(cars|motorbikes)',
    'id' => '\d+'
]);

// To indicate that the parameter is optional, 
// you need to put a question mark in front of it {?page}
Route::get('/news/{section}/{?page}', function($section, $page = 1){
    return 'Section: ' . $section . ' | Page: ' . $page;
});

Search route by name

use Extra\Routing\Router;

$router = Router::getInstance();
$route = $router->getRouteByName('news.list');

Generate route URL

use Extra\Routing\Router;
use Extra\Routing\Route;

$router = Router::getInstance();
Route::get('/news/{id}', 'action')->name('news.detail');

// Way 1 (using router)
$url = $router->generateRouteUrl('news.detail', ['id' => 86]); 

// Way 2 (using route)
$route = $router->getRouteByName('news.detail');
$url = $route->generateUrl(['id' => 86]); 

echo $url; // '/news/86';

router's People

Contributors

phpdevorg-extra avatar

Stargazers

空白格 avatar iextra avatar

Watchers

iextra avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.