Symfony 3 get all avaliable locales - symfony-3.3

Have some trouble getting all available locales in Symfony 3.
The idea is to create a custom language switcher.
I have
parameters:
locale: en
app.locales: en|fr|ru
I can get requested locale or user stored in session locale.
But how can i get all parameters.app.locales in Service of Controller on finally in Twig? So that i can have ar array like en|fr|ru and so on.
Thanks.

Because you have the locales specified as a parameter you can retrieve the parameter value depending on where you want it:
1. As service argument:
Pass the locales via constructor parameters by specifying in the service definition:
Service:
class SomeService {
private $locales;
public function __construct(array $locales)
{
$this->locales = $locales;
}
...
}
Service definition:
...\Service\SomeService:
arguments:
- '%app.locales%'
2. With the service container:
You can also retrieve parameters via the service container:
$locales = $this->container->getParameter('app.locales');
3. Pass locales to Twig
After getting the locales via above ways you can just pass them as variable just like normal Twig variables when rendering a template:
return $this->render('some.template.html.twig', [
'locales' => $locales,
]);
4. Set specific Twig parameters:
How to get config parameters in Symfony2 Twig Templates

Just came across this in Symfony 5.
Seems that there is no method for it in TranslatorInterface
Therefore the simplest way in my case would be to get all values from parameters
$locales = $this->getParameter('app_locales');
In my case
app_locales: en|fr|de|es|cs|nl|ru|uk|ro|pt_BR|pl|it|ja|id|ca|sl|hr|zh_CN|bg|tr|lt
defined in services.yaml

Related

Cant access helper methods inside mongoose schema

My directory looks like bellow
--controllers
-helper.js
--models
-userModel.js
--server.js
My helper module is like
module.exports = {
check: function() {
return 'check';
}
}
I want to access helper module inside userModel.js. So I put like
var helper = require('.././controllers/helper');
Then I do console.log(helper.check()); but it shows error helper.check is not a function Or if I do console.log(helper); only it returns {}. How to access the helper module inside models? Thank you.
Since you said it returns {}, can you please check in your helper module that you have imported userModel.js. Because it forms circular dependencies and sometimes result empty json.

Phalcon router sends the wrong parameters

I have a problem with my router in Phalcon.
I have an action in my controller which ether takes a date parameter or not.
So when I access an URL: http://example.com/sl/slots/index/2017-06-27
everything works ok.
But when I go to: http://example.com/sl/slots/index
I get the following error:
DateTime::__construct(): Failed to parse time string (sl) at position
0 (s): The timezone could not be found in the database.
So the router actually takes the "sl" in the beginning as a parameter.
My router for this kind of url is set like this:
$router->add(
"/{language:[a-z]{2}}/:controller/:action",
array(
"controller" => 2,
"action" => 3
)
);
Btw it does the same withut the index: http://example.com/sl/slots
Oh and my slots index action looks like this:
public function indexAction($currentDate = false){ //code }
So the $currentDate is set to "sl" when I call the action without a parameter
Thank you for the help
Well you need to add language in first argument of action too. Then it should work.
In addition to #Juri's answer.. I prefer to keep my Actions empty or as slim as possible. Imagine if you have 3-4 parameters in the Route, you will end up with something like:
public function indexAction($param1 = false, $param2 = false, $param3 = false....)
Here is how I prefer to handle Route parameters:
public function indexAction()
{
// All parameters
print_r($this->dispatcher->getParams());
// Accessing specific Named parameters
$this->dispatcher->getParam('id');
$this->dispatcher->getParam('language');
// Accessing specific Non-named parameters
$this->dispatcher->getParam(0);
$this->dispatcher->getParam(1);
...
}

Where can I set target step for failed purchase?

I am using custom checkout scenario which doesn't contain step "payment". However when purchase step fails, it tries to redirect to payment. Where can I change this behaviour?
Thanks.
Updated:
ChceckoutProcessScenario.php
class CheckoutProcessScenario implements ProcessScenarioInterface
{
public function build(ProcessBuilderInterface $builder)
{
$cart = $this->getCurrentCart();
$builder
->add('security', 'sylius_checkout_security')
->add('delivery', new Step\DeliveryStep())
->add('finalize', 'sylius_checkout_finalize')
->add('purchase', 'sylius_checkout_purchase')
;
$builder
->setDisplayRoute('sylius_checkout_display')
->setForwardRoute('sylius_checkout_forward')
->setRedirect('sylius_homepage')
->validate(function () use ($cart) {
return !$cart->isEmpty();
})
;
}
...
}
app/config/config.yml:
sylius.checkout.step.delivery.template: '#CoreBundle/Resources/views/Frontend/Checkout/Step/delivery.html.twig'
sylius.checkout_scenario.class: ZDG\CoreBundle\Checkout\CheckoutProcessScenario
and then there is checkoutStep, but those are only changed files.
I've looked into state-machine.yml, but it only defines states of order and payment, which i do not wish to modify.
So, the solution was les than expected: During pruchase step an event is triggered (sylius.checkout.purchase.complete), which has a listiner returning a response according to payment status. If status is not finished, it takes a parameter from container and redirects to provided url. This url is hardcoded in sylius/sylius/src/Sylius/Bundle/CoreBundle/Resources/config/services.xml:324
if you implemented custom class implementing ProcessScenarioInterface
There is also state machine configuration that could affect the redirection.
You will have to configure, what to do after each state change of the order.
Have a look in Sylius\CoreBundle\Resources\config\state-machine.yml
You can also check this for better understanding https://github.com/Sylius/Sylius/wiki/Status

Defaults in Symfony2 routing not being passed properly

I am currently trying to configure a routing option in Symfony2 so /cms will route to /cms/role/view. However, the passing of defaults doesn't seem to work properly.
/src/MyProject/CMSBundle/Resources/config/routing.yml
MyProjectCMS_specific:
pattern: /cms/{page}/{option}
defaults: { _controller: MyProjectCMSBundle:Main:index, page: role, option: view }
requirements:
_method: GET
/src/MyProject/CMSBundle/Controller/MainController.php
<?php
namespace MyProject\CMSBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class MainController extends Controller
{
public function indexAction($page, $option)
{
$response = null;
/* Switch statement that determines the page to be loaded. */
return $response;
}
}
?>
The problem is that when I try to go to `localhost/app_dev.php/cms', it gives me the following error:
Controller "MyProject\CMSBundle\Controller\MainController::indexAction()" requires that you provide a value for the "$page" argument (because there is no default value or because there is a non optional argument after this one).
500 Internal Server Error - RuntimeException
However, if I try to visit localhost/app_dev.php/cms/role or localhost/app_dev.php/cms/role/view, it gives me the correct page. I've tried adding a default route to /cms, but it still gives me the same error. How is this possible and how can I fix this?
Thanks in advance.
I don't know what is the difference between what you wrote and
public function indexAction($page = "role", $option = "view")
but maybe you could try it and tell us.

Multiple Zend_Mail configurations in application.ini

I'm am using Zend Framework.
I need to put multiple mail configurations in application.ini for Zend_Mail (using Zend_Application_Resource_Mail). Is it possible to do this using the standard classes in Zend Framework or do I need to create my own class?
I am using the latest stable version of Zend Framework.
Thanks for the answers
It does not appear to be possible to set multiple configurations for Zend_Mail with Zend_Application_Resource_Mail.
You could add the various configurations to application.ini but you will have to write your own class/functions to make the desired configuration active.
The things that are set by Zend_Application_Resource_Mail that you will have to override are Zend_Mail::setDefaultTransport($newTransport);, Zend_Mail::setDefaultReplyTo($email);, and Zend_Mail::setDefaultFrom($email);.
I tested something and found an easy thing you can do.
Set up your different configurations like this in application.ini:
mail_config.mail_test.transport.type = smtp
mail_config.mail_test.transport.host = "smtp.example.com"
mail_config.mail_test.transport.auth = login
mail_config.mail_test.transport.username = myUsername
mail_config.mail_test.transport.password = myPassword
mail_config.mail_test.defaultFrom.email = john#example.com
mail_config.mail_test.defaultFrom.name = "John Doe"
mail_config.mail_test.defaultReplyTo.email = Jane#example.com
mail_config.mail_test.defaultReplyTo.name = "Jane Doe"
Note how we are setting up options under mail_config. This will be the set of options to apply. mail_test is an example configuration. You can have multiple by setting mail_config.mail_test2, mail_config.corporate_mail, or mail_config.production etc.
Next, create an empty class that extends from Zend_Application_Resource_Mail. Preferably, it should be named and placed so it can be autoloaded.
The class:
<?php
class Application_Service_MailSettings extends Zend_Application_Resource_Mail { }
Now, here is how to override the default mail configuration easily with something else.
This example assumes you are in a controller:
// get the bootstrap, so we can get mail_config options
$bootstrap = $this->getInvokeArg('bootstrap');
$options = $bootstrap->getOption('mail_config');
// initialize the resource loader with the options from mail_config.mail_test
$mailSettings = new Application_Service_MailSettings($options['mail_test']);
$mailSettings->init(); // call init() so the settings are applied
// now the default transport, from, and reply to are set using mail_config.mail_test options.
// to use a different set of options, just do
// $mailSettings = new Application_Service_MailSettings($options['other_config');
This should accomplish what you want with very little new code.