Using $this when not in object context - in Laravel controller - php

I am using Laravel and I have just moved my code that is working locally into Live and am getting the exception below within my 'User' controller:
Unhandled Exception
Message:
Using $this when not in object context
The strange thing is, that this IS a class and works locally fine so I don't expect the solution to be using the static notation. It's only when I promote this to Live that I get this error. Could it be that something in the Laravel core that is missing from Live that is not loading the controller class properly?
Has anyone experienced this after promoting their code to Live? any ideas?
UPDATED: snippet of code where error is occuring. remember this code works locally so i believe something is missing rather than this code needs to be changed to fix this issue.
class User_Controller extends Base_Controller {
...
public function action_register() {
...
if ($user) {
//Create the Contact
DB::transaction(function() use ($user_id) {
$org = $this->create_org($user_id); //failing on this line with exception. btw $user is created fine
$this->create_contact($org->id);
$this->create_address($org->id);
});
private function create_org($user_id) {
$result = Org_type::where('name','=',$_POST['org_type'])->first();
$org = Org::Create(
array(
'name' => $_POST['org_name'],
'user_id' => $user_id,
'org_type_id' => $result->id,
)
);
return $org;
}
...

It seems the problem is that you're using $this inside a Closure you're providing to the DB::transaction function, i'm not sure why it would be working on live local, but you must import the instance of the controller into the function to use it.
The best way to do that, to avoid confusion, would be to alias it too, and possibly pass it by reference so you're not copying it, like:
$this_var = $this;
DB::transaction(function() use ($user_id, &$this_var as $controller) {
$org = $this->create_org($user_id); //failing on this line with exception. btw $user is created fine
$controller->create_contact($org->id);
$controller->create_address($org->id);
});
I'm not entirely sure if the syntax is perfect, but the logic is sound.

Related

Laravel - Error with objects and arrays

I'm working on a Laravel application that needs to export .xls reports, and for this I'm using the Laravel-Excel library. When I'm in the development environment, the flow happens normally. The problem happens in the production environment, when we try export the report, the system says I'm working with an object, not an array. But when I change the declaration to object, it accuses it is actually an array. I'm not understanding anything else. Has anyone ever had this? What is the solution?
This code is where the error occurs:
$excel->sheet("Sheet1", function ($sheet) use ($followupData) {
//some code above
$city = $paciente['demographic']['city'];
$sheet->cell('AK8', ExportCenter::TranslateOption($city->joinville));
//some code below
)->export('xls');
The function TranslateOption() :
private static function TranslateOption($value)
{
$options = new \App\Option();
$op = $options->where('value', $value)->first();
return ExportCenter::TratamentoBranco($op['label']);
}

{"status":false,"error":"Unknown method"} error CodeIgniter RESTful API

<?php
include(APPPATH.'/libraries/REST_Controller.php');
class Quiz extends REST_Controller{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function user_get()
{
$this->load->model('Quizmodel');
$data = $this->Quizmodel->getAll();
$this->response($data, 200);
}
function restclient()
{
$this->load->library('rest', array(
'server' => 'http://localhost/CodeIg/index.php/quiz/'
));
$userr = $this->rest->get('user','','json');
echo $userr;
}
}
?>
I am able to get JSON output if I type http://localhost/CodeIg/index.php/quiz/user in my browser, however if I type http://localhost/CodeIg/index.php/quiz/restclient it gives this error: {"status":false,"error":"Unknown method"}
I tried changing get to post but still the same error.
I referred this page https://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814 to do it.
You pinged me on GitHub, even though I haven't used or even thought about this code in at least 4 years.
https://github.com/chriskacerguis/codeigniter-restserver/blob/d19dc77f03521c7a725a4555407e1e4e7a85f6e1/application/libraries/REST_Controller.php#L680
This is where that error is being triggered. Throw a few breakpoints in there or var_dump()'s until you see what is causing the trouble.
You probably want to get off CodeIgniter though, and use something more actively maintained like SlimPHP or Lumen.
firstly I want as you have loaded rest api and created your controller quiz as an api to call , where you can only create your functions like user_get or restclient_get and access them the same manner you are doing.Just change you function name restclient to restclient_get then it will call instead it is even not running at this moment.

Laravel method attributesToArray() not running on production

I'm deploying my application to my production environment and it's not working as expected. I've narrowed the issue down to one line inside this loop in my controller;
foreach($temp_table_data as $a_payment) {
//array_push($payments, $a_payment->payment); //big collection object
array_push($payments, $a_payment->payment->first()->attributesToArray()); //smaller object
}
The error I get is call to a member function attributesToArray() on a non object. This seems crazy to be because - as the old saying goes - it works fine on my machine.
My dev. environment is Ubuntu trusty64 on PHP 5.5.21 and my production is RedHat Linux PHP 5.5.11. I thought these differences were very minor (maybe I'm wrong?).
If I do a print_r($temp_table_data() then I get a big collection returned. The same on both servers. So at some point it just stops liking either payment (that's a method) or first()
Here is partial of my TempTable.php Model with the payment method;
public function payment(){
return $this->hasMany('App\Models\Payment', 'Vendor ZIP', 'postcode');
}
And my Payment.php model (part of it);
class Payment extends Model {
protected $table = 'headquarters_data';
public function tempTable()
{
return $this->belongsTo('App\Models\TempTable', 'postcode', 'Vendor ZIP');
}
One of the tempTable models doesnt have a Payment and the attributesToArray() method is failing.
Try this and see if it works.
foreach($temp_table_data as $a_payment) {
$payment = $a_payment->payment->first();
if(!is_null($payment)){
$payments[] = $payment->attributesToArray();
}
}
the problem is that in production you have probably changed the data in your database and calling to first() method returns null then you are trying to call attributesToArray() on a null, which is wrong!
you should do a isset() function before calling attributesToArray().
if(isset($a_payment->payment->first()))
array_push($payments, $a_payment->payment->first()->attributesToArray());

Laravel resource controller testing gives NotFoundHttpException the second time

I am encountering a very strange thing while doing testing with Laravel 4. It looks like a bug but there's probably a logical explanation.
I have replicated the "bug" in a clean Laravel install and here's my code:
My resource controller /app/controllers/TestController.php:
(Created with php artisan controller:make TestController)
class TestController extends \BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
return Response::json(array());
}
// The end of the file is unchanged
In my app/routes.php:
Route::get('/', function()
{
return View::make('hello');
});
// Note the composed part of the URL.
// My problem isn't present if I just use `myapi` or `path` as a url
Route::resource('myapi/path', 'TestController');
Added in /app/test/ExampleTest.php:
public function testTest()
{
$res = $this->call('GET', 'myapi/path');
// Until here everything works right
$this->assertEquals(json_decode($res->getContent()), array());
// Now I call the same URL a second time
$res = $this->call('GET', 'myapi/path');
$this->assertEquals(json_decode($res->getContent()), array());
}
Now I run phpunit and here's what I get:
There was 1 error:
1) ExampleTest::testTest
Symfony\Component\HttpKernel\Exception\NotFoundHttpException:
/home/me/Web/laraveltest/bootstrap/compiled.php:5531
/home/me/Web/laraveltest/bootstrap/compiled.php:4848
/home/me/Web/laraveltest/bootstrap/compiled.php:4836
/home/me/Web/laraveltest/bootstrap/compiled.php:4828
/home/me/Web/laraveltest/bootstrap/compiled.php:721
/home/me/Web/laraveltest/bootstrap/compiled.php:702
/home/me/Web/laraveltest/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php:81
/home/me/Web/laraveltest/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:332
/home/me/Web/laraveltest/vendor/laravel/framework/src/Illuminate/Foundation/Testing/ApplicationTrait.php:51
/home/me/Web/laraveltest/app/tests/ExampleTest.php:25
In my other project I get a slightly different backtrace, but I have the impression that's the same problem: (but I have no idea of why the other is compiled and this one not)
2) UnitModelTest::testOther
Symfony\Component\HttpKernel\Exception\NotFoundHttpException:
/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:148
/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1049
/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1017
/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php:996
/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:775
/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:745
/home/me/Web/my-project/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php:81
/home/me/Web/my-project/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:327
/home/me/Web/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/ApplicationTrait.php:51
/home/me/Web/my-project/app/tests/UnitModelTest.php:32
In both case the line given in the trace for the test file corresponds to the second call of the test.
As I noted in the comments of the routes.php file, if I use a simple url with no slash, the test passes without problem.
I have no problem when I use the api from the browser.
I found many topics related to the NotFoundHttpException on StackOverflow, but none looks like mine. This one is specifically present when testing and only trigger an error at the second call.
So what am I doing wrong here ? Or is it really a bug ?
The problem is that calls made with the same client will use the provided URI relatively. That means what you actually call is:
myapi/path
myapi/myapi/path
You can fix this if you add a preface the urls with a / to make them absolute to the root of the application.
public function testTest()
{
$res = $this->call('GET', '/myapi/path');
// Until here everything works right
$this->assertEquals(json_decode($res->getContent()), array());
// Now I call the same URL a second time
$res = $this->call('GET', '/myapi/path');
$this->assertEquals(json_decode($res->getContent()), array());
}
If you experience other issues like that it often helps to call
$this->refreshApplication();
(This would also create a new client and therefore solve this issue as well)
In my case this error happened because i change public directory name to public_html my solution was put this in the \App\Providers\AppServiceProvider register method.
public function register()
{
// ...
$this->app->bind('path.public', function() {
return base_path('public_html');
});
}

how to fix CConsoleApplication.user undefined in a command class in yii framework?

I am having that error , whenever I ran my simple cron script in shell ,
any idea how to fix that thing ?, from the error itself, it says the .user is undefiend,
when I placed the
'user' => array(
// enable cookie-based authentication
'allowAutoLogin' => true,
'loginUrl' => array('myaccount/blah/login'),
in the console config, it is looking for a "Class" ,, what class am i supposed to include in that array? , this user login url is using an LDAP stuff in loggin in and authentication, what should I do ?
A CConsoleApplication is for handling offline tasks. For example, the application starts a cronjob within a linux system. The application checks each day if a registered user of your website should change his password, because it must be changed every 3 month. If the password expired send an email.
To preselecting the users you have set a scope condition to check the status of a user, as well as a scope to restricted signed in users on her own data:
public function scopes(){
return array(...,
'user' => array(
'condition'=>'id='.Yii::app()->user->id,
),
'active' => array(
'condition'=>'status='.self::STATUS_ACTIVE,
), ...
);
}
Now, in your CCA-Code you use the active scope to get all users:
$usersArray = User::model()->active()->findAll(); ...foreach.... The Problem here is in the use of the extended class, the CActiveRecord-class. Mostly used as a class extension in models, which are stored in a database. In this CActiveRecord-class the CActiveRecord->__call function is used to get all stored scopes of a model. After that the class merged the actually requested scopes with the rest of the database criteria. The fact that all scopes are loaded first occures the error in loading the user-scope, include Yii::app()->user->id. The WebUser is called and throws the exception 'CException' with message 'attribute "CConsoleApplication.user is not defined'. You wouldn't call the WebUser, but the automatism arrange this for you :-)
So, do it like schmunk says. Generate in your scope code an exception part where ensures that Yii::app()->user is not called:
public function scopes(){
if (Yii::app() instanceof CConsoleApplication) {
$user = array(''); //no condition
}else{
$user = array(
'condition'=>'id='.Yii::app()->user->id,
);
}
return array(
'user' => $user,
'active' => array(
'condition'=>'status='.self::STATUS_ACTIVE,
), ...
);
}
I hope the explanation helps and perhaps also for other problems.
Short answer: You can't use CWebUser in console application. Don't include it in your config/console.php
Long(er) answer: If you rely on a component, which needs CWebUser, you'll have to detect this in the component and create some kind of workaround for this case. Have a look at this code piece for an example how to detect, if you're running a console app.
Try this
public static $console_user_id;
public function init() {
if (Yii::app() instanceof CConsoleApplication) {
if (self::$console_user_id) $this->id = self::$console_user_id;
return false;
}
parent::init();
}
solved my problem by using update, instead of save in the script...no need to use user array and CWebUser class
I had the same problem. Screened all answers given here and found some good point, but solved my problem my way, although it may not be the best.
First off all I had to figure out that my Cron Jon threw the aforementioned Exception because inside the Cron job I was running a script which had this part of code in it
if(Yii::app()->user->isAdmin()) {...} else {...}
So the console threw the error since the user was not defined. I changed my code in such a way that I tested if the console was running it. The changed code is as follows:
$console = false;
try {
$test = Yii::app()->user->isAdmin();
}
catch (CException $e) {
$console = true;
}
if($console || (!$console && Yii::app()->user->isAdmin()) {...} else {...}
As said, not perfect, but maybe a solution for someone.

Categories