I create "BlocksHelper" in my custom plugin, but I can't use it without fatal errors. What is wrong?
plugin/myPlugin/src/View/Helper/BlocksHelper.php:
namespace myPlugin\View\Helper;
use Cake\View\Helper;
class BlockHelper extends Helper{
}
bootstrap.php:
...
Plugin::load(myPlugin, ['autoload' => true]);
View/AppView.php:
class AppView extends View
{
public function initialize()
{
parent::initialize();
$this->loadHelper('myPlugin.Blocks');
...
}
}
Related
I have implemented the below code for ssl implementation, but after that my redirect is no longer working:
Here is my code:
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
class WidgetsController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('Security', ['blackHoleCallback' => 'forceSSL']);
}
public function beforeFilter(Event $event)
{
$this->Security->requireSecure();
}
public function testRedirect()
{
**$this->redirect("/controller/Somewhere");**
}
public function forceSSL()
{
return $this->redirect('https://' . env('SERVER_NAME') . $this->request->getRequestTarget());
}
}
Any help is appreciated.
I am trying to add custom rule to form. I have added a custom function in model but it's not working for me.
class BackendUser extends ActiveRecord implements IdentityInterface
{
public function rules()
{
return [
['username','validateUsername','params'=>'username'=>'username']],
];
}
public function validateUsername($attribute, $params)
{
if (preg_match('/[^a-z])/i', $this->$attribute)) {
$this->addError($attribute, 'Username should only contain
alphabets');
}
}}
In PHP there is no construct like you used here (a => b => c, maybe it's a typo) and you don't have to pass any parameters anyway since you don't use them in the validator method. Simple
public function rules()
{
return [
['username','validateUsername'],
];
}
is enough.
There are few typos in your code. Try using $this->{$attribute} in dynamic attributes and also params key should be an array while calling inline validation.
class BackendUser extends ActiveRecord implements IdentityInterface
{
public function rules()
{
return [
['username','validateUsername','params'=>['username'=>'username']],
];
}
public function validateUsername($attribute, $params)
{
if (preg_match('/[^a-z])/i', $this->{$attribute})) {
$this->addError($attribute, 'Username should only contain alphabets');
}
}
}
I have installed the Bootstrap-UI plugin with composer and loaded the plugin in the bootstrap.php.
When I add the necessary code to the ViewApp.php I get an error.
Strict (2048): Declaration of App\View\AppView::initialize() should be compatible with Cake\View\View::initialize() [APP/View/AppView.php, line 22]
Code Context
*/
class AppView extends View
{
The code I added is:
class AppView extends View
{
public $layout = 'BootstrapUI.default';
public function initialize(array $config)
{
$this->loadHelper('Html', ['className' => 'BootstrapUI.Html']);
$this->loadHelper('Form', ['className' => 'BootstrapUI.Form']);
$this->loadHelper('Flash', ['className' => 'BootstrapUI.Flash']);
$this->loadHelper('Paginator', ['className' => 'BootstrapUI.Paginator']);
}
I had the same problem and I deleted the 'array $config' in the initialize function.
public function initialize()
{
// Code from Plugin
}
You also need to add <?php $this->extend('../Layout/TwitterBootstrap/dashboard'); ?> to your views to get the bootstrap look.
Im writing some tests with Yii2 and Codeception and I'm getting a weird error when using my own class in the _before() function:
FATAL ERROR. TESTS NOT FINISHED.
Class 'app\lib\FTPImage' not found in /var/www/proyect/tests/codeception/unit/lib/FTPImageTest.php:13
It is strange because if I use it in a test method it works like charm :/
Here is the code of the test:
<?php
namespace tests\codeception\unit\lib;
use Yii;
use yii\codeception\TestCase;
use app\lib\FTPImage;
class FTPImageTest extends TestCase{
public $ftp;
public function _before(){
$this->ftp = new FTPImage();
}
public function _after(){
}
public function testGetImagesNoImages(){
$ftp = new FTPImage();
$images = $ftp->getImages("123", "Foobar");
$this->assertEmpty($images);
}
}
And this is the code of the FTPImage class:
<?php
namespace app\lib;
use Yii;
use app\lib\FTPClient;
use \yii\base\Exception;
use \yii\base\ErrorException;
use \yii\imagine\Image;
class FTPImage{
public function __construct() {
}
public function getImages($reference, $supplier_name){
...
}
I hope there's someone who can help me
Thanks in advance!
Finally I found the solution. I've changed to _before and _after functions to setUp and tearDown. This way, I can use my FTPImage class.
Here is what I did:
class FTPImageTest extends TestCase{
public $ftp;
public function setUp(){
parent::setUp();
$this->ftp = new FTPImage();
}
public function tearDown(){
parent::tearDown();
}
...
}
In the document class, I got the following code:
package {
...
public class Main extends Sprite {
public function Main(){
...
model:IModel = new Model();
controller:IController = new Controller(model);
var controls:CompositeView = new Controls(model,controller)
//Accessing controls.x provokes 1202: Access of undefined property x in package view
}
}
}
Where class CompositeView is a extension of Component which in turn extends Sprite.
Component:
package view
{
....
public class Component extends Sprite
{
...
}
}
CompositeView:
package view
{
....
public class CompositeView extends Component
{
...
}
}
Controls:
package view
{
....
public class Controls extends CompositeView
{
...
}
}
But unfortunately, when I try to access controls.x I get a undefined property error.
Changing the package for all classes to the same name fixed this issue. E.g use package for everything instead of package and package.view.
What can be done in Actionscript to keep code organization and avoid this issue ?
Problem solved: var controls was used as a package name, changing name of the variable solved.