HI , i create a service to send data to a zebra printer , first create a third_party:
this is service :
When the printer not print i would show the error by flashdata
But the error is :
Object of class App\ThirdParty\StampaEtichette could not be converted to string
Third party class return a string ... why have i this error ?
Code:
<?php
namespace App\ThirdParty;
class StampaEtichette{
private $print_data;
public $error='';
public function __construct($print_data)
{
$this->print_data = $print_data;
$this->zpl();
}
public function zpl() :string {
$ip_stampante =getenv('IP_STAMPANTE');
log_message('debug', 'ip_stampante'.$ip_stampante);
$fp = @pfsockopen($ip_stampante, 6101 ,$errno, $errstr);
if(!$fp){
$errore = "$errstr ($errno)<br/>\n";
log_message('error', 'Errore apertura socket : '.$errore);
return $errore;
}else{
if(fputs($fp, $this->print_data)){
fclose($fp);
return 'stampato';
}else{
return 'Problemi Fput';
}
}
}
}
<?php
namespace App\ThirdParty;
class StampaEtichette{
private $print_data;
public $error='';
public function __construct($print_data)
{
$this->print_data = $print_data;
$this->zpl();
}
public function zpl() :string {
$ip_stampante =getenv('IP_STAMPANTE');
log_message('debug', 'ip_stampante'.$ip_stampante);
$fp = @pfsockopen($ip_stampante, 6101 ,$errno, $errstr);
if(!$fp){
$errore = "$errstr ($errno)<br/>\n";
log_message('error', 'Errore apertura socket : '.$errore);
return $errore;
}else{
if(fputs($fp, $this->print_data)){
fclose($fp);
return 'stampato';
}else{
return 'Problemi Fput';
}
}
}
}Code:
<?php
namespace Config;
use CodeIgniter\Config\BaseService;
use App\ThirdParty\StampaEtichette;
/**
* Services Configuration file.
*
* Services are simply other classes/libraries that the system uses
* to do its job. This is used by CodeIgniter to allow the core of the
* framework to be swapped out easily without affecting the usage within
* the rest of your application.
*
* This file holds any application-specific services, or service overrides
* that you might need. An example has been included with the general
* method format you should use for your service methods. For more examples,
* see the core Services file at system/Config/Services.php.
*/
class Services extends BaseService
{
public static function StampaEtichette(string $print_data,$getShared = true)
{
if ($getShared) {
return static::getSharedInstance('StampaEtichette',$print_data);
}
return new StampaEtichette($print_data);
}
}Code:
<?php
namespace App\Controllers\User;
use App\Controllers\BaseController;
use App\Models\Doa_righe_sparateModel;
use App\Models\DoaModel;
class UserStampaEtichette extends BaseController
{
public function stampa_id_righe_sparate($id)
{
$doa_righe_sparate_model = new Doa_righe_sparateModel();
$record = $doa_righe_sparate_model->find($id);
$data_to_print = $doa_righe_sparate_model->data_to_print($id);
//dd($data_to_print);
$stampa = service('StampaEtichette',$data_to_print);
if($stampa=='stampato'){
session()->setFlashdata('gestisciRecordOK', 'Stampa effettuata correttamente');
return redirect()->to('/user_Doa_righe_sparate/codiciSparati/'.$record->id_doa);
}else{
$errore = $stampa;
session()->setFlashdata('gestisciRecordBad', 'Problemi stampa :'. $errore);
return redirect()->to('user_Doa_righe_sparate/codiciSparati/'.$record->id_doa);
}
}
}Object of class App\ThirdParty\StampaEtichette could not be converted to string
Third party class return a string ... why have i this error ?