Use of undefined constant Laravel - laravel

Controller Code:
$tokenAmount = json_decode($token->getBody())->result;
return view('account')->with(compact('tokenAmount'));
View Code:
<label>Wallet Balance {{tokenAmount}}</label>
When I run the code. I'm getting following error.
Use of undefined constant tokenAmount - assumed 'tokenAmount'
PS: I tried following method also.
$tokenAmount = json_decode($token->getBody())->result;
return view('account', compact('tokenAmount'));
If I do echo $tokenAmount, it is printing the value without any error.

Please update your view code and check like:
<label>Wallet Balance {{tokenAmount}}</label>
to
<label>Wallet Balance {{$tokenAmount}}</label>

Related

Codeigniter 4 testing when controller method not found

I am doing HTTP testing in Codeigniter 4 and am getting the following error:
CodeIgniter\Exceptions\PageNotFoundException: Controller method is not found
The reason for this is I am trying to call a get() method on a route that only has a post (switching it to add results in a different response). That is correct. I am trying to make sure that the page returns 404 if someone tries to hack it. The problem is Codeigniter doesn't assert Status is 404 it simply falls over. Here is my code triggering:
$r = $this->withSession($session)->get($url);
$r->assertStatus(404);
Note the assert never runs! Instead I get the original error message. Surely I should be able to check for 404 routes (I've tried '404' instead of int) ?? Yes I could set a custom route that allowed Get but I want to test it as-is rather than what it isn't.
How do I get CI4 to accept my incorrect route as a 404?
My solution is to define a constant (IN_TESTING) for when I am testing and then edit system/Codeigniter run() function. This is of course amending the core system which some people won't like but until there is a fix (I am using 4.1.2) then this will suffice.
catch (PageNotFoundException $e)
{
if (!defined('IN_TESTING') || IN_TESTING == false){
$this->display404errors($e);
} else {
$this->router = Services::router($routes, $this->request);
$path = $this->determinePath();
$controller = $this->router->handle($path);
$method = $this->router->methodName();
echo $path."\n".$controller."\n".$method;
exit;
$this->response->setStatusCode(404);
return $this->response;
}
}
Key tip if you do make changes to system files, track them! If you then need to run an upgrade you'll know what changes you've made.

Undefined index:test in CodeIgniter

in Code Igniter i have declared some constants in constatnts.php like this
define('TEST',$_REQUEST['test']);
but they are not working and showing the error like Notice: Undefined index: testin C:\xampp\htdocs\userinterface\application\config\constants.php on line 39
what should i do?
The error is saying that the superglobal $_REQUEST does not have an item with the index of 'test'.
In other words, isset($_REQUEST['TEST'] === FALSE
You need to test that it is set before trying to use it to define the constant.
if(isset($_REQUEST['TEST']))
{
define('TEST',$_REQUEST['test']);
}
In this case, because the constant may not get defined, you always need to make sure it exists before you use it.
if(defined("TEST"))
{
echo TEST;
}

Firing an Event in Laravel 5

In an attempt to learn using events I have followed this tutorial
In my App\Events\ThingWasDone.php i have this
Session::put('testevent', 'it works!');
$author= Entity::find(261);
$author->still_active_URL = 99;
$author->save;
The Session is properly defined and I can get it in my View files.
Any attempt to write to DB produces this error:
LogicException in Model.php line 2632:
Relationship method must return an object of type
Illuminate\Database\Eloquent\Relations\Relation
I tried options like passin or not passing any variable:
\Event::fire(new ThingWasDone($object->id));
\Event::fire(new ThingWasDone());
Didn't help.
Any hint?
change
$author->save;
to:
$author->save();
This might solve the error.

No Error for Undefined Variable Used in Class Methods in PHP

I spent more than 10 hours to find out the typo for debugging my PHP program. I expected PHP would produce an error when using an undefined variable. But when it is used as an object in a method, it doesn't. Is there a reason for it?
<?php
$oClass = new MyClass;
// $oCless->tihs('key', 'taht'); //<-- triggers the error: Notice: Undefined variable
$oClass->tihs('key', 'taht');
echo $oClass->arr['key'];
class MyClass {
public $arr = array('key' => 'foo');
function tihs($key, $value) {
$tihs->arr[$key] = $value; //<-- this does not cause an error message.
}
}
?>
Normally if the error reporting level is set to E_ALL | E_STRICT (or E_ALL as of PHP 5.4.0) it should spit out an E_STRICT error. For instance, this code:
error_reporting(E_ALL | E_STRICT);
$tihs->adrr = 453;
Produces:
Strict Standards: Creating default object from empty value in [...]
Interestingly enough, if you specifically create an array instead of an ordinary variable as a property of an object that doesn't exist, e.g.:
error_reporting(E_ALL | E_STRICT);
$tihs->adrr[25] = 453;
No strict standards error is shown! It looks like this could potentially be something PHP folks might want to fix, because I'm not aware this is documented nor I think there's a legitimate reason for this behaviour.
For the record, in both cases regardless of the error a new stdClass is being created on the fly instead, like sberry mentions in his answer.
It is because of PHP trickery...
Under the covers, PHP is actually creating an object called tihs, adding an array to the object called arr and setting key to value.
Here is a print_r($tihs); after the assignment:
stdClass Object
(
[arr] => Array
(
[key] => taht
)
)
You seemed to have misspelt $oClass as $oCless
Agreed, $oCless instead of $oClass would give you an undefined variable error.
Also, "this" is a keyword in most languages and may be in php as well. You should refrain from using it so that it doesn't come out in other languages as a habit. You'll get way more errors if you're using "this" as function and variable names. You wouldn't even get things to compile.

Code Igniter Passing Variable to Controller Using URL Error

I am creatting a Code Igniter project in which I want to pass a variable through the URL like a get statement, like this:
url: /site/cake/1
controller function: cake($var)
but when the variable is left blank, I receive an error, how can I get code igniter, to ignore this?
In your controller, do this:
function cake($var = null) {
// your other code here
}
When $var isn't present in the URL, it will be set to null and you'll receive no error.
To explain why Colin's answer works:
The issue you had, was that there was no default value for that controller function. In php, creating a default value for a function parameter is done by assigning it a value in the function definition ($var = false). Now when the cake() function is called with no parameter, it will set $var to false by default.

Resources