Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @author Tomasz Marcinkowski
- * @since 31 10 2013
- */
- $path_base = '/symfony-components';
- spl_autoload_register(function ($class) {
- $pcr0_class_to_path = str_replace('\\', '/', $class);
- require __DIR__ . '/' . $pcr0_class_to_path . '.php';
- });
- ini_set('display_errors', 1);
- error_reporting(E_ALL);
- use Symfony\Component\Routing\Generator\UrlGenerator;
- use Symfony\Component\Routing\Matcher\UrlMatcher;
- use Symfony\Component\Routing\RequestContext;
- use Symfony\Component\Routing\RouteCollection;
- use Symfony\Component\Routing\Route;
- $routes = new RouteCollection();
- $routes->addPrefix($path_base);
- $routes->setHost('{subdomain}{_dot}szoper.net');
- $routes->addRequirements(array(
- 'subdomain' => '[a-z-]*',
- '_dot'=>'\.?',
- ));
- $routes->addDefaults(array('slash' => '/'));
- $routes->add('homepage', new Route('{slash}', array('controller' => 'HomepageController'), array(
- 'slash' => '/*',
- )));
- $routes->add('products', new Route(
- '/product/{product_id}',
- array('controller' => 'ProductController'),
- array(
- 'product_id' => '[0-9]*',
- )
- ));
- $routes->add('posts', new Route(
- '/post/{post_id}',
- array('controller' => 'PostController'),
- array(
- 'post_id' => '[0-9]*',
- 'subdomain' => '[a-z-]*',
- '_dot'=>'\.?',
- ),
- array(),
- '{subdomain}{_dot}szoper.net'
- ));
- $context = new RequestContext('', $_SERVER['REQUEST_METHOD'], $_SERVER['HTTP_HOST']);
- $matcher = new UrlMatcher($routes, $context);
- $generator = new UrlGenerator($routes, $context);
- $path_info = preg_replace('!^' . $path_base . '!', '', $_SERVER['REQUEST_URI']);
- try {
- $parameters = $matcher->match($path_info);
- var_dump($parameters);
- } catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
- printf('oops! no controller found for: %s', $path_info);
- }
- $link = $generator->generate('products', array('product_id' => rand(1,999)), true);
- echo sprintf('<br>Random product link: <a href="%s">%s</a>', $link, $link);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement