Welcome Guest, Not a member yet? Register   Sign In
Return error from service
#1

(This post was last modified: 07-13-2024, 07:16 AM by pippuccio76.)

HI , i create a service to send data to a zebra printer , first create a third_party: 

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';
           
            }

           
           
        }
       
           


    }






}
this is service  :

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);
      }
   
}
When the printer not print i would show the error by flashdata
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);
        }     


    }



}
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 ?
Reply
#2

__construct I don't have to return anything. Learn the PHP syntax
You should use the method after (new StampaEtichette())->zpl() or try __invoke()
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

(This post was last modified: 07-13-2024, 08:14 AM by pippuccio76.)

(07-13-2024, 07:39 AM)ozornick Wrote: __construct I don't have to return anything. Learn the PHP syntax
You should use the method after (new StampaEtichette())->zpl() or try __invoke()

change third party :

Code:
<?php
namespace App\ThirdParty;


class StampaEtichette{

private $print_data;

    public $error='';

    public function __construct($print_data)
    {
    $this->print_data = $print_data;

    }


    public function zpl()  {

        $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';
           
            }

           
           
        }
       
           


    }






}

now service :
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))->zpl();
      }
   
}


service(): Return value must be of type ?object, string returned

(07-13-2024, 07:48 AM)pippuccio76 Wrote:
(07-13-2024, 07:39 AM)ozornick Wrote: __construct I don't have to return anything. Learn the PHP syntax
You should use the method after (new StampaEtichette())->zpl() or try __invoke()

change third party :

Code:
<?php
namespace App\ThirdParty;


class StampaEtichette{

private $print_data;

    public $error='';

    public function __construct($print_data)
    {
    $this->print_data = $print_data;

    }


    public function zpl()  {

        $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';
           
            }

           
           
        }
       
           


    }






}

now service :
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))->zpl();
      }
   
}


service(): Return value must be of type ?object, string returned

found solution , service mast return nothing only an istance of class

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);
      }
   
}

in controller :

Code:
        $stampa_etichette =  service('StampaEtichette',$data_to_print);

        $stampa = $stampa_etichette->zpl();
Reply




Theme © iAndrew 2016 - Forum software by © MyBB