In a controller, I need to return translated message in an Ajax json response as:
$body['message'] = __("Duplicated");
$this->response->body(json_encode($body));
$this->response->statusCode(202);
$this->response->type('json');
return $this->response;
But the translation is not looked up. However, in a template, I am able to get this working:
<?= __("Duplicated");?>
I know I can use the Ajax layout and write a template, but in this case the message body is very short, I need to return a Status Code other than 200. How can I do this in a controller in CakePHP 3?
It seems to be a CakePHP bug (Not a bug, see comments below). I have in app.php
'App' => [
...
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'en_US'),
...
]
and in AppController::beforeRender(), According to http://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html#changing-the-locale-at-runtime I could override with this (but in fact that does not work):
I18n::locale('zh');
When I change config/app.php to
'App' => [
...
'defaultLocale' => env('APP_DEFAULT_LOCALE', 'zh'),
...
]
It works.
Related
I’m getting error “Undefined class Meta” on my laravel blog application. Also it doesn't provides the title on output. On html output I get blank . Is it problem because of Undefined class Meta? If then how can I define that class on blade engine? Any idea?
Check screen shot to understand
Looks like you haven't installed the package but you're trying to use it.
First, type composer require eusonlito/laravel-meta
Then, in your config/app.php add:
'providers' => [
'...',
Eusonlito\LaravelMeta\MetaServiceProvider::class
];
'aliases' => [
'...',
'Meta' => Eusonlito\LaravelMeta\Facade::class,
];
Then retry. Your app should work
I have a CakePHP 3.3.14 application where I've created 2 subdirectories, webroot/data/downloads/ and webroot/data/master
I want to put these paths in a custom configuration file and reference them in a Controller. But I can't see how to do this.
I've followed the documentation on Configuration but it's not very clear.
So what I've done:
Created config/my_config.php
The above file defines an array:
return [ 'downloadsPath' => 'webroot/data/downloads/', 'masterPath' => 'webroot/data/master/' ];
In config/bootstrap.php I've put: Configure::load('my_config', 'default');
How do I then use this in a Controller? If I put Configure::read('my_config.masterPath'); it gives an error saying: Class 'App\Controller\Configure' not found
If I add use Cake\Core\Configure; to the top of my Controller, that clears the error but the return value is null:
debug(Configure::read('my_config.masterPath')); // null
Loading another config file just extends the default App.config. So just use \Cake\Core\Configure::read('masterPath') and you are good.
EDIT
If it is your goal to have different config paths you could do it like this:
// my_config.php
return [
'MyConfig' => [
'masterPath' => '...',
...
]
];
Then use the config like this:
<?= \Cake\Core\Configure::read('MyConfig.masterPath') ?>
Using Laravel 5.2.41
Using following translation package: mcamara/laravel-localization
Link inside my view :
Edit Link
routes.php files inside Lang/fr & Lang/nl
<?php
return [
'account-edit' => "account/wijzig-gegevens/{id}",
];
<?php
return [
'account-edit' => "donnees/modifier-donnees/{id}",
];
Laravel routes file:
Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => ['localize','localeSessionRedirect', 'localizationRedirect' ]
], function()
{
Route::get(LaravelLocalization::transRoute('routes.account-edit'),'AccountController#edit');
});
When i look at the link in my default language (nl) i get the correct link like so:
Edit Link
But when i change my language to french i get following link:
Edit Link
Can't figure out why this is happening
I was looking into the code of that package.
It seems to me if I'm not mistaken, that the logic regarding the translation of URL's is based on the route name, not the route path.
You are using the route path here:
LaravelLocalization::getLocalizedURL(null, trans('routes.account-edit'), ["id" => Auth::user()->id])
But it seems like you should actually use the route name instead, meaning 'account-edit' in this case.
I wanna use smarty template engine in yii2.
In my project, i need load view codes from database and render them from controller.
My question is this:
Is there any way to render a view code from string and control it like common render?
i need something like below:
$this->renderAsString($templateStr, ['param1'=>$val1, 'param2'=>$val2]);
this is important for me can access variable and function like as below code in index.tpl file.
$this->render('index.tpl'['param1'=>$val1, 'param2'=>$val2]);
I read this http://www.smarty.net/docs/en/resources.string.tpl but my answer is different, i think.
There is special separate extension called yii2-smarty for rendering views with Smarty. You need to install it via Composer, then configure like this for usage:
return [
//....
'components' => [
'view' => [
'renderers' => [
'tpl' => [
'class' => 'yii\smarty\ViewRenderer',
//'cachePath' => '#runtime/Smarty/cache',
],
],
],
],
];
As for your specific problem, look at these two issues on Github:
Add ability to render view from string
View renderer from db - not implemented yet
Core developer Klimov Paul recommends to use eval, but also in Smarty dedicated function exists exactly for these kind of situations.
Example 8.4. Another {eval} example
This outputs the server name (in uppercase) and IP. The assigned
variable $str could be from a database query.
<?php
$str = 'The server name is {$smarty.server.SERVER_NAME|upper} '
.'at {$smarty.server.SERVER_ADDR}';
$smarty->assign('foo',$str);
?>
Where the template is:
{eval var=$foo}
I try to use one copy of Yii2 as multisite engine (one CMS, several domain-depended configs).
But when I send some ActiveForm in a controller action, I see Bad Request 400.
When I add _csrf field in simple HTML form and try to send it in an action, it works fine.
How can I resolve it?
Here is a solution: I just name session with unique string for every domain.
This is done by editing domain-depended configs.
return [
'components' => [
...
'session' => [
'name' => 'domain_name_session',
],
...
],
];