symfony link to change language and stay on the page - php

I would like to make a link the will be in the layout to change the language. So it should work for many routes.
for example
i'm on the page /en/myModule
and the links should point to
/de/myModule
/fr/myModule
I found a solution here :http://oldforum.symfony-project.org/index.php/m/70452/
<?php echo link_to(
'Germany',
'#default?' . http_build_query(array(
'sf_culture' => 'de',
'module' => $sf_request->getParameter('module'),
'action' => $sf_request->getParameter('action'))
), null, '&')) ?>
Problem is that I need a default route, and I don't want to have it.
Is there any solution for what I need ?

routing:
user_switch_culture:
url: /culture-change/:language
param: { module: user, changeCulture }
In your layout template:
<?php echo link_to(image_tag("flags/gb.gif"), "user_switch_culture", array("language"=>"en", "redirect"=>$sf_request->getUri())) ?>
Link will generate:
http://example.com/culture-change/fr?redirect=http//example.com/fr/control-panel
In your action:
public function executeChangeCulture(sfWebRequest $request)
{
$oldCulture = $this->getUser()->getCulture();
$newCulture = $request->getParameter("language");
$this->getUser()->setCulture($newCulture);
return $this->redirect(str_replace('/'.$oldCulture.'/', '/'.$newCulture.'/', $request->getParameter("redirect")));
}
Just off the top off my head. should work...
Not great: Should do some filter on the redirect to make sure it's the correct domain name etc.

Why not make a specific action for this?
public function executeChangeLanguage(sfWebRequest $request)
{
if (in_array($request->getParameter('lang'), sfConfig::get('app_site_languages'))
{
$this->getUser()->setCulture($request->getParameter('lang'));
}
// you can ask the browser for referrer or send a parameter to the change language action
// something like '/change-language?lang=ro&redirect=your page'.
// if you are sending a redirect parameter you must make sure that it's actually a page within your site
$referrer = $request->getReferer();
// or $referrer = $request->getParameter('redirect');
// you can further check the referrer here
return $this->redirect($referrer);
}

I think that I have a solution:
$uri = sfContext::getInstance()->getRouting()->getCurrentRouteName();
echo link_to('French', $uri, array('sf_culture'=>'fr')) . ' | ';
echo link_to('English', $uri, array('sf_culture'=>'en')) . ' | ';
echo link_to('German', $uri, array('sf_culture'=>'de'));
Is it a good one or is there a better solution ?

Related

pass multiple parameters to controller from route in laravel5

I want to pass multiple parameters from route to controller in laravel5.
ie,My route is ,
Route::get('quotations/pdf/{id}/{is_print}', 'QuotationController#generatePDF');
and My controller is,
public function generatePDF($id, $is_print = false) {
$data = array(
'invoice' => Invoice::findOrFail($id),
'company' => Company::firstOrFail()
);
$html = view('pdf_view.invoice', $data)->render();
if ($is_print) {
return $this->pdf->load($html)->show();
}
$this->pdf->filename($data['invoice']->invoice_number . ".pdf");
return $this->pdf->load($html)->download();
}
If user want to download PDF, the URL will be like this,
/invoices/pdf/26
If user want to print the PDF,the URL will be like this,
/invoices/pdf/26/print or /invoices/print/26
How it is possibly in laravel5?
First, the url in your route or in your example is invalid, in one place you use quotations and in the other invoices
Usually you don't want to duplicate urls to the same action but if you really need it, you need to create extra route:
Route::get('invoices/print/{id}', 'QuotationController#generatePDF2');
and add new method in your controller
public function generatePDF2($id) {
return $this->generatePDF($id, true);
}

Rewriting route depending of parameter value Yii

I have several rules in Yii that allows me to rewrite some routes, where every will be pass to the action as a get parameter.
'<department>' => 'products/index',
'<department>/<category>' => 'products/index',
I want to explicitly write a rule that depending of the parameter value will change the url to whatever I want
example, right now I have an URL like this
www.mysite.com/Books+%26+Pencils which was rewritten because of this rule '<department>' => 'products/index', which is ok
I want to change that URL to www.mysite.com/books-pencils , if anyone know how to write a rule that compares the value of the deparment attribute and then rewrites it to whatever I want.
THanks
You can use a custom class to handle you special requests.
I have used sth like this, to get my custom URLs out of a database:
'urlManager'=>array(
'rules'=>array(
array(
'class' => 'application.components.UrlRule',
),
),
),
Then you create your custo class similar to this:
<?php
Yii::import("CBaseRule");
class UrlRule extends CBaseUrlRule
{
public function createUrl($manager,$route,$params,$ampersand)
{
// check for my special case of URL route, if not found, then return the unchaged route
preg_match("/^(.+)\/(.+)$/", $route, $r);
if(!is_array($r) or !isset($r[1]) or !isset($r[2])) {
return $route;
}
// handle your own route request, and create your url
$url = 'my-own-url/some-thing';
// check for any params, which i also want to add
$urlParams = $manager->createPathInfo($params,"=","&");
$return = trim($url,'/');
$return.= $urlParams ? "?" . $urlParams : "";
return $return;
}
public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
// handle my special url request
$controller = '....';
$action = '.....';
// return the controller/action that should be used
return lcfirst($controller)."/".$action;
}
}
I do not know if this was what you wanted, but at least in this class you can do everything you need with the URL requested.
If you would e.g. like to redirect a lot of similar URLs with a 301 Redirect to 1 URL, you could think of sth like this in the parseUrl function
// check my route and params, and if I need to redirect
$request->redirect('/your/new/url/?params=bla',true,'301');
First of all, if you want to change a URL, you should do a redirect (in this case 301). To implement this logic you can use custom URL rule class.
Url manager configuration:
'rules' => array(
// custom url rule class
array(
'class' => 'application.components.MyUrlRule',
),
)
MyUrlRule class:
class MyUrlRule extends CBaseUrlRule
{
public function createUrl($manager,$route,$params,$ampersand)
{
// Logic used to create url.
// If you do not create urls using Yii::app()->createUrl() in your app,
// you can leave it empty.
}
public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
// modify url
$pathInfoCleaned = strtolower(preg_replace('+%26+', '-', $pathInfo));
// redirect if needed
if ($pathInfo !== $pathInfoCleaned) {
$request->redirect($pathInfoCleaned, true, 301);
}
// parse params from url
$params = explode('/', $pathInfo);
if (isset($params[0])) {
$_GET['department'] = $params[0];
if (isset($params[1])) {
$_GET['category'] = $params[1];
}
}
return 'products/index';
}
}

Cakephp routing controller alias

I'm trying to do the same as this site, stackoverflow, do with their URLs.
CakePHP works like this: website/controller/action/
I want to config routing to achieve this:
myWebSite.com/questions/(question_id)/(question)/
eg: myWebSite.com/questions/12874722/cakephp-routing-controller-alias /
I didnt figured it out how to do this bold part of URL.
In your Config/routes.php
Router::connect('/questions/*', array('controller' => 'questions', 'action' => 'view'));
In Controller/QuestionsController.php
view action get question id as
public function view() {
$question_id = isset($this->request->params['pass'][0]) ? $this->request->params['pass'][0] : "";
$question = isset($this->request->params['pass'][1]) ? $this->request->params['pass'][1] : "";
if( empty($question_id) ) {
throw new NotFoundException('Could not find that question');
}
// if $question is empty then get slug from database and redirect to /question_id/question
// Get question details and set
}

CakePHP not redirecting after opening new window with JavaScript

In one of my actions I'm adding items to a shopping cart on an external website via a javascript window. After they're added then I redirect back to a home page, however, CakePHP isn't redirecting. The items are being added to the cart correctly.
//OrdersController
function place_filled_orders($id = null){
$this->layout = false;
$this->autoRender = false;
?>
<script>
cart_window = window.open("http://www.example.com/load_cart_with_stuff");
cart_window.close();
</script>
<?
$this->redirect(array('controller' => 'orders', 'action' => 'home'));
}
When I click on the link that corresponds to this action, it just stays on /orders/place_filled_orders rather than redirecting to /orders/home
You can't add scripts in your Controller that way. It's totally against MVC rules and you should therefor avoid it. You should add a view (or element) that does both actions:
So add a app/View/Orders/place_filled_order.ctp file with something like this:
<?php
echo $this->Html->scriptBlock('
cart_window = window.open("http://www.example.com/load_cart_with_stuff");
cart_window.close();
window.location.href = "' . $this->webroot . '/orders/home";
');
Edit
At second glance, it actually looks like you are looking for the requestAction method instead. So you'll get your controller to look like:
function place_filled_orders($id = null) {
$this->autoRender = false;
$this->requestAction('/load_cart_with_stuff');
$this->redirect(array('controller' => 'orders', 'action' => 'home'));
}

php, how to send a manual post within the zend framework?

im not sure if the question was framed correctly, but here is my situation:
i have two actions: indexAction and searchAction
a third action looks something like this:
public function customsearchAction()
{
$request = $this->getRequest();
if($request->isPost()){
$category = $request->getParam('select_category');
$searchString = $request->getParam('header_search_form');
if($category == 'index'){
$this->_redirector->gotoSimple('index', 'index', null,
array('term' => $searchString )
);
}
if($category == 'search'){
$this->_redirector->gotoSimple('search', 'index', null,
array('term' => $searchString )
);
}
}
}
this is fine and dandy, the only problem is that the redirect adds the term as a get string instead of a post like i need it.
any ideas?
Browser redirect will always add term to GET for next request to process. What you can do here is use ZF MVC internal redirect using 'forward' .
$this->_forward('search','index',null,array('term' => $searchString ));
Inside your searchAction
$searchString = $this->_getParam('term');

Categories