PHP code example of piotrpolak / conditional-routing-bundle
1. Go to this page and download the library: Download piotrpolak/conditional-routing-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/ */
piotrpolak / conditional-routing-bundle example snippets
// in AppKernel::registerBundles()
$bundles = array(
// ...
new PiotrPolak\ConditionalRoutingBundle\PiotrPolakConditionalRoutingBundle(),
// ...
);
namespace MyApp\Router;
use PiotrPolak\ConditionalRoutingBundle\Model\AbstractYamlRouteResolver;
class TimeCampaignRouteResolver extends AbstractYamlRouteResolver
{
/**
* {@inheritdoc}
*/
public function resolveBundleNames()
{
// Loads @BaseCampaignBundle/Resources/config/routing.yml
// In most cases it makes no sense to define bundle names that are ALWAYS loaded here as it can be done in the
// app/config/routing.yml
$bundleNames = ['BaseCampaignBundle'];
if ((int)date('Y') >= 2016) {
// Loads @MyCampaign2016Bundle/Resources/config/routing.yml
// Can overwrite routes defined in BaseCampaignBundle or any other bundle
$bundleNames[] = 'MyCampaign2016Bundle';
}
return $bundleNames;
}
}
namespace MyApp\Router;
use Doctrine\ORM\EntityManagerInterface;
use PiotrPolak\ConditionalRoutingBundle\Model\AbstractYamlRouteResolver;
class DatabaseCampaignRouteResolver extends AbstractYamlRouteResolver
{
/** @var EntityManagerInterface */
private $em;
/**
* CampaignRouteResolver constructor.
* @param EntityManagerInterface $em
*/
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* {@inheritdoc}
*/
public function resolveBundleNames()
{
// Loads @<CURRENT_BUNDLE_NAME>/Resources/config/routing.yml
// Can overwrite any previously defined routes
$bundleNames = [$this->getCurrentBundleName()];
return $bundleNames;
}
/**
* @return string
*/
protected function getCurrentBundleName()
{
// Suppose you have a an entity having two fields: key and value
// You might want to add some kind of cache to avoid reading from DB at every request
return $this->em->getRepository('MyApp:Parameter')
->findOneBy(['key' => 'currentBundle'])
->getValue();
}
}
namespace MyApp\Router;
use PiotrPolak\ConditionalRoutingBundle\Model\RouteResolverInterface;
use PiotrPolak\ConditionalRoutingBundle\Model\RoutingDefinition\XmlBundleRoutingDefinition;
use PiotrPolak\ConditionalRoutingBundle\Model\RoutingDefinition\YamlBundleRoutingDefinition;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class VariousTypesCampaignRouteResolver implements RouteResolverInterface
{
/** @var EntityManagerInterface */
private $em;
/** @var SessionInterface */
private $session;
/**
* VariousTypesCampaignRouteResolver constructor.
* @param EntityManagerInterface $em
* @param SessionInterface $session
*/
public function __construct(EntityManagerInterface $em, SessionInterface $session)
{
$this->em = $em;
$this->session = $session;
}
/**
* {@inheritdoc}
*/
public function resolveConditionalRoutingDefinitions()
{
$definitions = [];
// Overwrites homepage for the first visit
$numberOfHits = $this->session->get('my_app.number_of_visits', 0);
$this->session->set('my_app.number_of_visits', $numberOfHits + 1);
if (0 === $numberOfHits) {
$definitions[] = new YamlBundleRoutingDefinition('MyAppFirstVisitBundle');
}
// Disables some of the business critical routes based on the database value
if ($this->em->getRepository('MyApp:Parameters')->findIsMaintenanceModeOn()) {
$definitions[] = new XmlBundleRoutingDefinition('MyAppMaintenanceModeBundle');
}
return $definitions;
}
}
sh
rm -f ./app/cache/*/*UrlGenerator__*.php* && rm -f ./app/cache/*/*UrlMatcher__*.php*
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.