For php version 5.3. This line is working fine.
<?php echo CController::createUrl('/Reload/data')?>
But when I updated my php to 5.4. I am getting
Non-static method CController::createUrl() should not be called statically, assuming $this from incompatible context
What should I have to do to make it work in yii?
You need to access a controller object. Assuming you are working within a view you can use $this to access the current controller object:
<?php $this->createUrl('/Reload/data')?>
Within a widget you can use $this->controller:
<?php $this->controller->createUrl('/Reload/data')?>
If not, you can use Yii::app()->controller:
<?php Yii::app()->controller->createUrl('/Reload/data')?>
I can't figure how this line would work even in PHP 5.3 since you are calling a non static method in a static context. CController::createUrl() actually references $this...
Anyway, back to your main point : in Yii1 you can also manage your URLs with CApplication::createUrl(), and the Application object is available anywhere in your code.
Related
I have TestController, hich doesn't have its own model. I use various model inside it, among them Trunk Model. I have my function "call", that wants to use method "singlePckgCall" from Trunk Model. Until here everything goes okay.
This method, which I'm trying to use, uses another method, placed below - "callSingleNumber".
And then it stops, and sends me an error page:
Unknown Method – yii\base\UnknownMethodException
Calling unknown method:
frontend\controllers\TestController::callSingleNumber()
I have no idea why it wants to call a method from TestController, instead on Trunk Model, as I want it to do. In result, it doesn't see such a method, because it exists in another class. I tried to rewrite this part manually once more, but it didn't help.
You are calling singlePckgCall statically, so inside singlePckgCall method $this instance is not available.
To solve your problem, you can
1) call callSingleNumber statically in singlePckgCall :
$action_id_array[$key] = self::callSingleNumber($numery[$i], TRUE);
at row 52
2) create an instance of Trunk class, so $this is available in singlePckgCall:
$instance = new Trunk();
$instance->singlePckgCall($numery);
In a custom validation function in my model class. I need to use javascript code. for that i used registerJs function but i am getting error:-
Calling to undefined function registerJs()
I also tried calling it by including View class i.e., View::registerJs() but it is also giving error called
Non-static method yii\web\View::registerJs() should not be called statically, assuming $this from incompatible context
How can i user Javascript in Yii2 model class.
Edit:
I have created a custom function for mobile number validation and calling that function from rules section of model. Now i want to use javascript code in that function. is there any other way to achive it?
Thanks in advance
That method is not static. If you open the view.php of the framework you can check out the implementation.
public function registerJs($js, $position = self::POS_READY, $key = null){..
}
Exception clearly mentions that should not be called statically because it is not declared static.
I have seen few of the implementations which call this method as:
$view->registerJs($js, $view::POS_END);
Basically loading a particular JS file in one of the functions.
It is not a good idea using Javascript with a model. If you go that way then probably after some time you will find you heading into big problems with the architecture of the app.
The best way is to call the model inside a controller and then interact with the controller through the Javascript code.
I am getting below error in my project.
Non-static method illuminate\Http\Request::all() should not be called statically . assuming $this from incompatible context
You haven't provided any code sample, but from the sounds of the error, looks like you're trying to call the all() method of Illuminate\Http\Requestusing the $this->all() syntax, but the method is static.
You might have a class extending the Request class, if that's the case you need to call it like self::all() or parent::all()
I'm using the laravel's package
"pingpong/widget": "~2.1",
I've set the configuration in config\app.php
Added
Pingpong\Widget\WidgetServiceProvider::class,
and
'Widget' => Pingpong\Widget\WidgetFacade::class,
And as in the documentation, i'm using this code to create a widget:
use Pingpong\Widget\Widget;
Widget::register('small', function($contents)
{
return "<small>{$contents}</small>";
});
But I got this error:
Non-static method Pingpong\Widget\Widget::register() should not be called statically, assuming $this from incompatible context
In the Pingpong\Widget\Widget class, there are no static functions, so of course this error is expected. But... how we can create a widget then, when all sample codes in the documentation are using static classes and in the Widget class, there are no static methods? Am I missing something?
Creating a widget object and calling the ->register() on it also throw an error, because creating a widget expects BladeCompiler $blade, Container $container in the _construct method and I have no idea what to pass here.
Any ideas on what could be wrong with this implementation?
Change use Pingpong\Widget\Widget; to just use Widget;
Laravel, and some packages, include Facades to facilitate easy usage of some components. While everything looks like a static call, the static call on the facade actually maps to a non-static call on a concrete object that is masked by the facade. So, when you're attempting Widget::register(), the facade looks up the concrete instance it has of the Widget class, and then calls the register() method on that instance non-statically.
In your config/app.php file, you've added the Widget alias which points to the WidgetFacade. This alias is created in the base namespace. If you would like to use this alias to the facade, you need to just reference the Widget alias in the base namespace (i.e. use Widget;).
By stating use Pingpong\Widget\Widget;, your Widget::register() call is trying to call a static register() method on the Pingpong\Widget\Widget class, instead of the method on the facade.
I don't usually call methods statically but since working with the Yii framework I have started to more.
I'm experiencing an issue which I haven't come across before, I am doing this from a SignupForm class:
$send = mail::sendMail($email_data);
..inside the sendMail method which is obviously inside the mail class I have this line:
$email_data['message'] = $this->sanitizeMsg($email_data['message']);
The sanitizeMsg is a method of the mail class and hence that's why I thought referencing it via $this should work.
However I am getting the error:
Calling unknown method: app\models\SignupForm::sanitizeMsg()
Why is it looking for it in the SignupForm class? Does this have something to do with me calling it statically? Do I need to revert to using self:: instead or should I stop calling it statically?
Using $this only works for member methods. I.e. you can only use it from an instance of the class and I believe also only to access instance members.
To access static members, you should use self::.
Not familiar with the Yii framework, but what someone does is to use static methods as factory methods. Maybe that's what you're confused by. The static method then creates a new instance of itself and returns it, and inside that instance you can of course use $this, but not in the static method. Static methods have no $this.