How to change Template's route by conditions in Controller? - cakephp-3.0

I'm a newbie using cakephp-3.0.
I'm planing to show different templates-views according to user's browser/agent.
In cakephp2.x the code can be like below:
if ($this->DisplayModeService->hasSpViewSupport()) {
App::build([
'View' => [APP . 'View/SmartPhone/', APP . 'View/'],
]);
}
but in cakephp3.0 it's in app.php :
return [
'App' => [
'paths' => [
'templates' => [
APP . 'Template' . DS . 'SmartPhone' . DS,
APP . 'Template' . DS,
],
],
],
];
But I want to change the template route only if the page has smartPhone version.
In the case above it jump into SP version anyway.
(for I wanna keep the same name for the same page )
ex:
/Template/SmartPhone/profile.ctp, /Template/profile.ctp.(has SP/PC Version)
/Template/news.ctp (PC Version only)
can it be possible?

You would better use $this->request->is('mobile'), and Themes

Related

How to allow DIV tags in Yii2 Editor

I installed a blog module that allows me to add blogs to my Yii2 app. Everything works well except the editor. I think Yii2 by default uses redactor.
The problem is that when I add code with using the code interface, all DIV tags are converted automatically to P tags.
I have checked through the redactor configuration but it does not look like there is a setting to adjust this:
'redactor' => [
'class' => 'yii\redactor\RedactorModule',
'uploadDir' => '#frontend/images/blog/upload',
'uploadUrl' => '/sites/eop/frontend/images/blog/upload',
'imageAllowExtensions' => ['jpg', 'png', 'gif', 'svg'],
],
Any idea on where else to look at?
You need to use the replaceDivs option, and set it to false under the client options. See the below code for an example
<?php echo \yii\redactor\widgets\Redactor::widget(
[
'model' => $model,
'attribute' => 'body',
'clientOptions' => [
'replaceDivs' => false
]
]
);
?>
if you are using an ActiveForm it should be like
<?php echo $form->field($model, 'body')->widget(
[
'clientOptions' => [
'replaceDivs' => false
]
]
);
?>

Yii2 decimal format with decimalSeparator comma

I'm currently working with Yii2 framework and I need to set the default decimal separator symbol ',' instead of '.'. Searching on the internet I find this solution that doesn't work:
'formatter' => [
'class' => 'yii\i18n\Formatter',
'thousandSeparator' => '.',
'decimalSeparator' => ','
],
I put those lines in the web.php file in the 'components' section.
Do you have any further suggestion?
Your settings look fine. You just have to make sure your output is handled by the Formatter. Like this... (try in your code)
echo Yii::$app->formatter->asDecimal(1234567.12); // => 1.234.567,12
The Yii-framework format variables by using such methods "behind the scenes".

Time is wrong when get datetime in yii2

I have a problem when I get datetime in my yii2 project. When I get datetime, the date is true but the time is wrong. I execute my code and the result is :
2016-05-02 12:30:28
whereas the time in my laptop is : 19:30. What's the problem? I use time in Indonesia. This is my code:
$time = new \DateTime('now', new \DateTimeZone('UTC'));
$model->tanggal_sampai = $time->format('Y-m-d H:i:s');
First, Find out the timezone for Indonesia from List Of Supported TimeZones - php Manual
Then, make it common for all places using config.php file. Add 'timeZone'=>'Your TimeZone', after components section.
Example : config.php
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
.
.
.
],
'timeZone'=>'Asia/Kolkata',
'params' => $params,
];
You are asking for the time in the UTC time zone with new \DateTimeZone('UTC'). Either ask for it in your own time zone, which I think is WIB:
$time = new \DateTime('now', new \DateTimeZone('WIB'));
or without a time zone:
$time = new \DateTime('now');

How to use Yii without jQuery?

Unfortunately, the company I work for already has a lot of client-side code written for Mootools, and they don't seem to like each other very much. :S
No matter what I do, I can't seem to stop jQuery getting included in every page. Any ideas?
By default Yii application templates make use of AppAsset
class AppAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'css/site.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
that depends on YiiAsset asset bundle, and Yii built-in widgets use jQuery heavily. Remove 'yii\web\YiiAsset' dependency if you don't plan to use Yii client-side features.
go to your AppAsset.php and remove yii\web\YiiAsset.
also, go to your main layout file, remove the default NavBar because it will call jquery.js to render
#remove the default main menu below
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/site/login']] :
['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']],
],
]);
NavBar::end();
This will remove jquery.js from Yii2. I would recommend you to switch from MooTools to Jquery if you can. It makes your life alot easier.
from docs you can disable the jQuery asset bundle by associating false to it :
return [
// ...
'components' => [
'assetManager' => [
'bundles' => [
'yii\web\JqueryAsset' => false,
],
],
],
];
But you need to consider that many jQuery built in widgets will stop working.
I don't know much about Mootools, but if it supports AJAX and you are building a complete fronted on top of it then you may also consider using Yii as a REST Service.

ZF2 ZendSkeleton why using '__NAMESPACE__' key in default route?

in ZF2 skeleton, router configuration uses a key :
'__NAMESPACE__'
precisely :
'__NAMESPACE__' => 'Application\Controller',
cf:
https://github.com/zendframework/ZendSkeletonApplication/blob/master/module/Application/config/module.config.php#l32
We tried in our modules router config to use without quote:
__NAMESPACE__ => 'Application\Controller',
but it seems to break configuration.
why do we use quote instead of
__NAMESPACE__
to get its value ?
because by default, config files hasn't namespace declared. Config parser can read string
'__NAMESPACE__'
and determine correctly the namespace.
If you want to use it without quotes, you can declare in your config file :
namespace Application;
and use __NAMESPACE__ without quote.
That's why you can see sometimes in tutorials for Doctrine config's sample like :
'doctrine' => array(
'driver' => array(
'application_entity' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'paths' => __DIR__ . '/../src/' . __NAMESPACE__ . '/Entity',
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => 'application_entity',
)
)
)
),