Unknown error when running command line Phalcon app - php

I'm trying to run a command-line task, and my cli.php file is giving me this error:
PHP Notice: Array to string conversion in /var/www/htdocs/classschedule/app/cli.php on line 23
PHP Fatal error: Uncaught RuntimeException: Call to undefined method ::gettaskname() in /var/www/htdocs/classschedule/app/cli.php:23
Stack trace:
#0 /var/www/htdocs/classschedule/app/cli.php(23): Phalcon\Cli\Console->handle(Array)
#1 {main}
thrown in /var/www/htdocs/classschedule/app/cli.php on line 23
Here is my cli.php
include '/var/www/common/dump.php';
require 'config/bootstrap.php';
$DI->get('dispatcher')->setDefaultNamespace('Task');
$DI->get('dispatcher')->setNamespaceName('Task');
$Console = new \Phalcon\CLI\Console();
$Console->setDI($DI);
$arguments = [];
foreach($argv as $k => $arg) {
if($k == 1) {
$arguments['task'] = $arg;
} elseif($k == 2) {
$arguments['action'] = $arg;
} elseif($k >= 3) {
$arguments['params'][] = $arg;
}
}
try{
$Console->handle($arguments); // <-- This is line 23
}
catch(\Phalcon\Exception $e){
echo $e->getMessage();
exit(255);
}
I have no idea why either the Notice or Fatal error are getting generated. This file is almost identical to the cli.php for another app I have, that runs just fine. Even taking out the foreach() still causes the error.
Edit:
Bootstrap.php
Config.php
Solved
Solution:
My DI, Dispatcher, and Router were all MVC versions instead of their CLI equivalents. Changing them fixed the problem - setTask() was expected in the Dispatcher.

Could you please share your config/bootstrap.php file? I tested with:
use Phalcon\Di\FactoryDefault\Cli as DI;
Parameters were read and line 23 was asking for MainTask handler class (no error).
This is the code I tested:
use Phalcon\Loader;
use Phalcon\Di\FactoryDefault\Cli as CliDI;
$DI = new CliDI();
$loader = new Loader();
$loader->registerNamespaces(
[
'Task' => __DIR__ . '/tasks',
]
);
$loader->register();
$Console = new \Phalcon\CLI\Console();
$Console->setDI($DI);
$arguments = [];
foreach($argv as $k => $arg) {
if($k == 1) {
$arguments['task'] = $arg;
} elseif($k == 2) {
$arguments['action'] = $arg;
} elseif($k >= 3) {
$arguments['params'][] = $arg;
}
}
try{
$Console->handle($arguments);
}
catch(\Phalcon\Exception $e){
echo $e->getMessage();
exit(255);
}
And MainTask.php:
namespace Task;
use Phalcon\Cli\Task;
class MainTask extends Task
{
public function mainAction()
{
echo 'This is the default task and the default action' . PHP_EOL;
}
public function testAction(array $params)
{
echo sprintf('hello %s', $params[0]);
echo PHP_EOL;
echo sprintf('best regards, %s', $params[1]);
echo PHP_EOL;
}
}

$Console->handle($arguments); // <-- This is line 23
It seems that this line is expecting a string and you're passing an array.
Maybe phalcon is not handling this case well and can't instantiate some other object on which it tries to call gettaskname on.

Related

PHP Recoverable fatal error - Catch not working

I have code that I am trying to update to work with at least PHP version 7.3 and even when I put in a try catch statement I still get a fatal error thrown.
Below is the function that puts Objects into the Session. line causing error: $objects[$i] = $Object;
function findObjectsLIB() {
$objects = ""; $i = 0;
foreach ($_SESSION['Objects'] as $o => $Object) {
$className = get_class($Object);
if (!empty($className)) {
try {
$objects[$i] = $Object;
$i++;
} catch(Exception $err) {
$_SESSION['errors'] .= $err->getMessage();
}
}
}
return $objects;
}
I am getting this error.
PHP Recoverable fatal error: Object of class Extra could not be
converted to string.
How it not being caught?
For one it is strange that it is not being caught and why is it having an issue in 7.3 but not 5.6?

Upgraded PHP & Symfony, now getting "Call to a member function render() on null" using Twig

I recently upgraded PHP from 7.4 to 8.2 and with that, I've upgraded Symfony, Twig to the current versions. (Twig 3.5) Unfortunately that has now broken my application. I've managed to fix everything up till now but I'm stuck with the Template and and it's function to render pages using Twig.
The error is:
Fatal error: Uncaught Error: Call to a member function render() on null in /var/www/html/website/lib/Template.php:79
Stack trace:
#0 /var/www/html/website/lib/Handler.php(44): MyProject\Template::render()
#1 /var/www/html/website/lib/Handler.php(61): MyProject\Handler::error()
#2 [internal function]: MyProject\Handler::exception()
#3 {main}
thrown in /var/www/html/website/lib/Template.php on line 79<
This is on the initial index page to the website. The code in question is:
public static function render($template, $args = [], $returnOutput = false)
{
$arg['user'] = Auth::user();
$output = self::$twig->render($template . '.twig', $args);
if (getenv('STAGE') == 'dev') {
$output = self::$purifierExtension->getLog() . $output;
}
if ($returnOutput) {
return $output;
} else {
echo $output;
}
}
The page that is calling this function is this one:
MyProject\Template::header('MyProject', true);
$user = MyProject\Auth::user();
$args = [
'error' => false,
'redirect' => isset($_GET['redirect']) && $_GET['redirect'] ? $_GET['redirect'] : 'main.php',
'username' => ''
];
if (!$user && isset($_POST['username'])) {
MyProject\Form::validateCsrf('login');
$username = $_POST['username'];
$passwrd = $_POST['password'];
// query for matching user/password
try {
$MyProjectuserid = MyProject\Auth::login($username, $passwrd);
} catch (UnauthorizedHttpException $ex) {
$args['error'] = 'Could not login. Please try again.';
$args['username'] = $username;
}
}
if (MyProject\Auth::user()) {
MyProject\Alert::redirect($args['redirect']);
}
MyProject\Template::render('index', $args);
MyProject\Template::footer();
I have checked file permissions, I have removed and reinstalled the packages using Composer, and I have commented out various lines, leaving just this line as the culprit:
$output = self::$twig->render($template . '.twig', $args);
Any suggestions or help you can offer will be most gratefully received!
Additional info:
Thanks for your comment. I confess I have inherited this site and am not familiar with Symfony/Twig (I'm more of a dba person). The only initialisation I can find are these:
in Template.php
private static $twig;
self::$twig = new Twig_Environment($loader);
self::$twig->addFunction($fn);
self::$twig->addExtension(self::$purifierExtension);
self::$twig->addExtension(self::$answerTypeExt);
$output = self::$twig->render($template . '.twig', $args);
return self::$twig;
and in Handler.php
public static function init()
{
$loader = new Twig_Loader_Filesystem('templates');
self::$twig = new Twig_Environment($loader);
This site used to work until a recent PHP/Symfony stack upgrade. So is this something in the logic used to be acceptable but isn't any more?
Bootstrap.php loads various files:
require('vendor/autoload.php');
use Symfony\Component\Dotenv\Dotenv;
$dotenv = new Dotenv();
$dotenv->load(DIR.'/../.env');
MyProject\Handler::init();
MyProject\DB::init();
MyProject\Auth::init();
MyProject\Template::init();
MyProject\Form::init();

Codeigniter dompdf : Invalid Character Error

When I try to print a pdf I get this error:
An uncaught Exception was encountered
Type: DOMException
Message: Invalid Character Error
Filename: /home/ireto/domains/ireto.be/public_html/madicbelgium/application/helpers/dompdf/lib/html5lib/TreeBuilder.php
Line Number: 3191
Backtrace:
File: /home/ireto/domains/ireto.be/public_html/madicbelgium/application/helpers/dompdf/lib/html5lib/TreeBuilder.php
Line: 3191
Function: setAttribute
File: /home/ireto/domains/ireto.be/public_html/madicbelgium/application/helpers/dompdf/lib/html5lib/TreeBuilder.php
Line: 1493
Function: insertElement
File: /home/ireto/domains/ireto.be/public_html/madicbelgium/application/helpers/dompdf/lib/html5lib/Tokenizer.php
Line: 2456
Function: emitToken
File: /home/ireto/domains/ireto.be/public_html/madicbelgium/application/helpers/dompdf/lib/html5lib/Tokenizer.php
Line: 1102
Function: emitToken
File: /home/ireto/domains/ireto.be/public_html/madicbelgium/application/helpers/dompdf/src/Dompdf.php
Line: 470
Function: parse
File: /home/ireto/domains/ireto.be/public_html/madicbelgium/application/helpers/dompdf_helper.php
Line: 26
Function: loadHtml
File: /home/ireto/domains/ireto.be/public_html/madicbelgium/application/controllers/admin/Estimates.php
Line: 136
Function: pdf_create
File: /home/ireto/domains/ireto.be/public_html/madicbelgium/index.php
Line: 293
Function: require_once
This is my code on line :3191
->
private function insertElement($token, $append = true) {
$el = $this->dom->createElementNS(self::NS_HTML, $token['name']);
if (!empty($token['attr'])) {
foreach ($token['attr'] as $attr) {
if (!$el->hasAttribute($attr['name']) && preg_match("/[^A-Za-z0-9]/u", $attr['name'])) {
$el->setAttribute($attr['name'], $attr['value']);
}
}
}
if ($append) {
$this->appendToRealParent($el);
$this->stack[] = $el;
}
return $el;
}
if you face an error like An uncaught Exception was encountered the best way is, to actually catch the exception, because you get the answer for your error in your Exception
the following code snippet should do what i mean
private function insertElement($token, $append = true) {
try
{
$el = $this->dom->createElementNS(self::NS_HTML, $token['name']);
if (!empty($token['attr'])) {
foreach ($token['attr'] as $attr) {
if (!$el->hasAttribute($attr['name']) && preg_match("/[^A-Za-z0-9]/u", $attr['name'])) {
$el->setAttribute($attr['name'], $attr['value']);
}
}
}
if ($append) {
$this->appendToRealParent($el);
$this->stack[] = $el;
}
return $el;
}
catch (DOMException $e)
{
echo '<strong>Errormessage:</strong>'.$e->getMessage().'<br />';
echo $e->getTraceAsString();
}
}
if you have an error now, you should see the exact information in order to fix this error, something like the following occurs:
Errormessage: Invalid Character Error
#0 [...][...](7): DOMElement->setAttribute('1pro-1', 'someValue')
#1 {main}DOMException Object

PHP Exceptions CodeIgniter

my code is generating ssse following error:
Fatal error: Uncaught TypeError: Argument 1 passed to
CI_Exceptions::show_exception() must be an instance of Exception,
instance of Error given, called in
C:\xampp\htdocs\gog\lib\core\Common.php on line 662 and defined in
C:\xampp\htdocs\gog\lib\core\Exceptions.php:190 Stack trace: #0
C:\xampp\htdocs\gog\lib\core\Common.php(662):
CI_Exceptions->show_exception(Object(Error)) #1 [internal function]:
_exception_handler(Object(Error)) #2 {main} thrown in C:\xampp\htdocs\gog\lib\core\Exceptions.php on line 190
The line of the codes described is these:
function _exception_handler(Throwable $exception)
{
$_error =& load_class('Exceptions', 'core');
$_error->log_exception('error', 'Exception: '.$exception->getMessage(), $exception->getFile(), $exception->getLine());
// Should we display the error?
if (str_ireplace(array('off', 'none', 'no', 'false', 'null'), '', ini_get('display_errors')))
{
$_error->show_exception($exception); //line 662
}
exit(1); // EXIT_ERROR
}
public function show_exception(Exception $exception) //line 190
{
$templates_path = config_item('error_views_path');
if (empty($templates_path))
{
$templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
}
$message = $exception->getMessage();
if (empty($message))
{
$message = '(null)';
}
if (is_cli())
{
$templates_path .= 'cli'.DIRECTORY_SEPARATOR;
}
else
{
set_status_header(500);
$templates_path .= 'html'.DIRECTORY_SEPARATOR;
}
if (ob_get_level() > $this->ob_level + 1)
{
ob_end_flush();
}
ob_start();
include($templates_path.'error_exception.php');
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
}
Someone can pinpoint my problem ?
No CodeIgniter version has that _exception_handler() signature. This is what happens when you modify stock framework files.
Download a fresh copy of the latest CodeIgniter and replace yours with it.
You call show_exception(Exception $exception) from within _exception_handler(Throwable $exception). Since the former takes an Exception as argument, you cannot give it a Throwable, as you do in line 662. Exception implements the Throwable interface, but that does not guarantee all Throwables to be Exceptions (e. g. they could be of type Error).
Replace _exception_handler(Throwable $exception) with _exception_handler(Exception $exception), or change show_exception(Exception $exception) to show_exception(Throwable $exception), and change the method bodies accordingly if needed.

php mvc router not working

i have php mvc project .
This is my router class.
router class job is include controller in main index page of website.
its work on localhost but when i uploaded on my host not working
what i do ?
<?php
class route
{
function __construct()
{
if(empty($_GET['url']))
{
$url = "index";
}
else
{
$url = $_GET['url'];
}
$url = explode("/",$url);
if(!empty($url[0]))
{
$controllername = "controllers/".$url[0].".php";
if(file_exists($controllername))
{
require($controllername);
$object = new $url[0];
//$object->loadmodel($url[0]);
if(!empty($url[1]))
{
if(method_exists($object,$url[1]))
{
if(!empty($url[2]))
{
$object->$url[1]($url[2]);
}
else
{
$object->$url[1]();
}
}
else
{
echo "Error";
}
}
else
{
echo "Error";
}
}
else
{
echo "Error";
}
}
}
}
this is my error in error_log
thrown in /home/mogeir/public_html/libs/route.php PHP Notice: Array
to string conversion in /home/mogeir/public_html/libs/route.php on
line 33 PHP Notice: Undefined property: admin::$Array in
/home/mogeir/public_html/libs/route.php on line 33 PHP Fatal error:
Uncaught Error: Function name must be a string in
/home/mogeir/public_html/libs/route.php:33
Use the following function call forms instead - the commented ones are correct alternatives too. See call_user_func and call_user_func_array.
if (!empty($url[2])) {
$object->{$url[1]}($url[2]);
// Alternative 1
// call_user_func(array($object, $url[1]), $url[2]);
// Alternative 2
// call_user_func_array(array($object, $url[1]), array($url[2]));
} else {
$object->{$url[1]}();
// Alternative 1
// call_user_func(array($object, $url[1]));
// Alternative 2
// call_user_func_array(array($object, $url[1]), array());
}
You said:
its work on localhost but when i uploaded on my host not working what i do ?
That has to do with the error reporting activation or with difference in PHP version.

Categories