PHP code example of webiny / router

1. Go to this page and download the library: Download webiny/router 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/ */

    

webiny / router example snippets


    Router::setConfig($pathToYourConfig);

class MyClass
{
	use \Webiny\Component\Router\RouterTrait;

	function __construct(){
		$result = $this->router()->match('blog/tag/some_tag');
		$result = $this->router()->match('http://www.webiny.com/blog/post/some-post/12');
	}
}

class MyClass
{
	use \Webiny\Component\Router\RouterTrait;

	function __construct(){
		$result = $this->router()->match('blog/post/12/comments');
		if($result){
		    $callbackResult = $this->router()->execute($result);
		}
	}
}

class MyClass
{
	use \Webiny\Component\Router\RouterTrait;

	function __construct(){
		$url = $this->router()->generate('BlogTag',  ['tag'=>'html5', 'page'=>1]);
		// http://www.webiny.com/blog/tag/html5/?page=1
		// page was not defined in the route, that's why it's appended as a query param

		$url = $this->router()->generate('BlogAuthor');
		// http://www.webiny.com/blog/authors/webiny
		// the value of "author" was not set, so the generator used the default value
	}
}
yaml
    Router:
        Routes:
            BlogTag:
                Path: blog/tag/{tag}
                Callback: MyApp\Blog\showTag
            BlogComments:
                Path: blog/post/{id}/comments
                Callback:
                    Class: MyApp\Blog
                    Method: showComments
            BlogPost:
                Path: blog/post/{slug}/{id}
                Callback: MyApp\Blog\showPost
                Options:
                    slug:
                        Pattern: .*?
            BlogAuthor:
                Path: blog/authors/{author}
                Callback: MyApp\Blog\showAuthorPosts
                Options:
                    author:
                        Default: webiny
            Blog:
                Path: blog
                Callback: MyApp\Blog\index