PHP code example of proklung / wp-symfony-router-bundle

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

    

proklung / wp-symfony-router-bundle example snippets


class SampleAjaxController extends AbstractWPAjaxController
{
    public function action()
    {
        $this->checkTypeRequest('Invalid type request');

        $response = new Response(
            'OK',
            Response::HTTP_OK
        );

        $response->headers->set('Content-Type', 'application/html; charset=utf-8');
        $response->send();
        
        wp_die();
    }
}

use Prokl\WpSymfonyRouterBundle\Services\Agnostic\RoutesConfigurator;
use Prokl\WpSymfonyRouterBundle\Services\Agnostic\Router;
use Prokl\WpSymfonyRouterBundle\Services\Agnostic\WpInitializerRouter;

$agnosticRouter = new RoutesConfigurator(
    $_SERVER['DOCUMENT_ROOT'] . '/local/configs/standalone_routes.yaml',
    $_SERVER['DOCUMENT_ROOT'] . '/wp-content/cache/routes', // Кэш; если null - без кэширования.
    $_ENV['APP_DEBUG'] // Режим отладки или нет
);

$agnosticRouterInstance = new Router(
    $agnosticRouter->getRouter(),
    new WpInitializerRouter()
);

$router = \Prokl\WpSymfonyRouterBundle\Services\Agnostic\RoutesConfigurator::getInstance();

    public function load(array $configs, ContainerBuilder $container) : void
    {
        // ....
         $this->loadRoutes(__DIR__ . '/../Resources/config', 'routes.yaml');
    }

    /**
     * Загрузить роуты в бандле.
     *
     * @param string $path   Путь к конфигу.
     * @param string $config Конфигурационный файл.
     *
     * @return void
     *
     * @throws InvalidArgumentException Нет класса-конфигуратора роутов.
     */
    private function loadRoutes(string $path, string $config = 'routes.yaml') : void
    {
        $routeLoader = new \Symfony\Component\Routing\Loader\YamlFileLoader(
            new FileLocator($path)
        );

        $routes = $routeLoader->load($config);

        if (class_exists(InitRouter::class)) {
            InitRouter::addRoutesBundle($routes);
            return;
        }

        throw new InvalidArgumentException('Class InitRouter not exist.');
    }