1

I have 'sendsms' function which i used it in one of my controllers and worked fine. now what i need to know how i can make class reference of this code to use it in other controllers, instead of copy/paste whole code in all controllers. In other Q/A they mentioned about only creating reference but i wanted to do it properly like using constructor or etc, not just doing things work, i want to do it like real-world project.

Here's the code in controller :

    public function store(Request $request)
{
    $this->validate($request,[
        'title' => 'required|string|min:6',
        'gametype' => 'required|string|min:3',
        'description' => 'required|string|min:1|max:180',
        'price' => 'required|numeric|min:4',
        'buyyer_id' => 'required|numeric|min:1'
//            'seller_id' => 'required|numeric|min:1'
    ]);

//        return RequestModel::create([
//            'title' => $request['title'],
//            'description' => $request['description'],
//            'gametype' => $request['gametype'],
//            'price' => $request['price'],
//            'buyyer_id' => $request['buyyer_id'],
//            'seller_id' => Auth::user()->id,
//        ]);
//
    $requestModel = new RequestModel;
    // store
    $requestModel->title       = $request['title'];
    $requestModel->description      = $request['description'];
    $requestModel->gametype = $request['gametype'];
    $requestModel->price       = $request['price'];
    $requestModel->buyyer_id      = $request['buyyer_id'];
    $requestModel->seller_id = Auth::user()->id;
    $requestModel->save();

    return $this->sendSms($request['title'], $request['gametype']);


}
// I want to use this code in another class to use it in all controllers without copy/paste it.
function sendSms($reqid, $recgametype) {

    //Send sms to getway
    //implement later.

    $otp_prefix = ':';
    $response_type = 'json';

    $textMSGATLAS = iconv("UTF-8", 'UTF-8//TRANSLIT',"req : ( " .$reqid. " ) for ( " .$recgametype.  " ) submitted ");

    ini_set("soap.wsdl_cache_enabled", "0");
    try {
        $client = new SoapClient("http://xxxx");
        $user = "user";
        $pass = "pass";
        $fromNum = "+xxx";
        $toNum = "+xxxx";
        $messageContent = $textMSGATLAS;
        $op  = "send";

        $client->SendSMS($fromNum,$toNum,$messageContent,$user,$pass,$op);

    } catch (SoapFault $ex) {
        echo $ex->faultstring;
    }


}

I'm right now learning and I'm beginner at this so help to understand how to make it work properly. Thanks.

Mohammad Eskandari
  • 221
  • 1
  • 5
  • 14
  • Possible duplicate of [how to create global function that can be accessed from any controller and blade file](https://stackoverflow.com/questions/44021662/how-to-create-global-function-that-can-be-accessed-from-any-controller-and-blade) – caiovisk Feb 14 '19 at 01:54
  • @caiovisk in top duplicate they only mention about creating a class, which it's not enough. i wanted something just like what I've got below. – Mohammad Eskandari Feb 14 '19 at 02:22

1 Answers1

2

You can create a separate SMS class like :

<?php 

namespace App;

class SMS {

    private $reqid;
    private $recgametype;

    public function __construct($reqid, $recgametype)
    {
        $this->reqid = $reqid;
        $this->recgametype = $recgametype;
    }

    public function send()
    {
        $otp_prefix = ':';
        $response_type = 'json';

        $textMSGATLAS = iconv("UTF-8", 'UTF-8//TRANSLIT',"req : ( " .$this->reqid. " ) for ( " .$this->recgametype.  " ) submitted ");

        ini_set("soap.wsdl_cache_enabled", "0");

        try {
            $client = new SoapClient("http://xxxx");
            $user = "user";
            $pass = "pass";
            $fromNum = "+xxx";
            $toNum = "+xxxx";
            $messageContent = $textMSGATLAS;
            $op  = "send";

            return $client->SendSMS($fromNum,$toNum,$messageContent,$user,$pass,$op);

        } catch (SoapFault $ex) {

           throw new \Exception('SMS sending failed')
        }
    }

}

And then inside controller or wherever you would need :

public function sendSms($reqid, $recgametype) {

    $sms = new \App\SMS($reqid, $recgametype);

    $sms->send();


}

You can also create custom exception like SMSSendingFailedException and throw it instead of standard \Exception inside send() function.

That will help you to send appropriate response in controller like :

public function sendSms($reqid, $recgametype) {

    try{

        $sms = new \App\SMS($reqid, $recgametype);

        $sms->send();

        return response()->json('message' => 'SMS sent successfully', 200);
    }
    catch(SMSSendingFailedException $e){

        return response()->json('message' => 'SMS sending failed', 500);
    }


}

Then to go one step further, you can use concept of laravel facade if you need it all over the project with a quick class alias.

Mihir Bhende
  • 8,677
  • 1
  • 30
  • 37
  • Hey Man. Thanks for Respond, This just what i wanted. Amazing, Organized, and worked. is there any more tip about how i executed return in controller? since i'm learning i'm doing things to just work, if u found any problem, just mention, i'm gonna google it. Thanks. – Mohammad Eskandari Feb 14 '19 at 02:19