Symfony URL Redirection: Difference between revisions
Jump to navigation
Jump to search
Created page with "Some time is very needy to redirect URL base on different conditional behaviour. In this example present how to redirect to local <syntaxhighlight lang="php"> namespace Chork..." |
No edit summary |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
Some time is very needy to redirect URL base on different conditional behaviour. | Some time is very needy to redirect URL base on different conditional behaviour. This example presents how to check locale and redirect to locale | ||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
namespace Chorke\Bundle\WebappBundle\Controller; | namespace Chorke\Bundle\WebappBundle\Controller; | ||
| Line 5: | Line 5: | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | |||
use Symfony\Component\HttpFoundation\Response; | use Symfony\Component\HttpFoundation\Response; | ||
/** | /** | ||
* @Template | |||
* @Route("/home") | * @Route("/home") | ||
*/ | */ | ||
| Line 34: | Line 36: | ||
} | } | ||
$parameters = array('locale' => $_locale); | $parameters = array('locale' => $_locale); | ||
return | return $parameters; | ||
} | } | ||
Latest revision as of 16:14, 8 January 2018
Some time is very needy to redirect URL base on different conditional behaviour. This example presents how to check locale and redirect to locale
namespace Chorke\Bundle\WebappBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Response;
/**
* @Template
* @Route("/home")
*/
class HomeController extends Controller
{
private $lingos;
private $locals;
public function __construct()
{
$lingos = "ar|bn|en|fr|tk|zz";
$this->lingos = str_replace("|zz", "", $lingos);
$this->locals = explode('|', $this->lingos);
}
/**
* @Route("/{_locale}",
* name="home_index", defaults={"_locale"="zz"},
* requirements={"_locale"="ar|bn|en|fr|tk|zz"})
*/
public function indexAction($_locale)
{
if(!$this->hasLocal($_locale))
{
return $this->redirectToLocal('home_index');
}
$parameters = array('locale' => $_locale);
return $parameters;
}
private function hasLocal($locale)
{
return in_array($locale, $this->locals);
}
private function redirectToLocal($route)
{
$parameters = array('_locale' => 'en');
return $this->redirectToRoute($route, $parameters, Response::HTTP_TEMPORARY_REDIRECT);
}
}