【问题标题】:How to add my function and use it properly in controller?如何添加我的功能并在控制器中正确使用它?
【发布时间】:2019-02-14 01:50:41
【问题描述】:

我有 'sendsms' 功能,我在我的一个控制器中使用它并且工作正常。现在我需要知道如何使该代码的类引用以在其他控制器中使用它,而不是在所有控制器中复制/粘贴整个代码。 在其他 Q/A 中,他们提到只创建引用,但我想像使用构造函数等一样正确地做到这一点,而不仅仅是做事,我想像现实世界的项目一样做。

这是控制器中的代码:

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


}

我现在正在学习,我是这方面的初学者,所以请帮助了解如何使其正常工作。谢谢。

【问题讨论】:

标签: php laravel


【解决方案1】:

您可以创建一个单独的 SMS 类,例如:

<?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')
        }
    }

}

然后在控制器内部或您需要的任何地方:

public function sendSms($reqid, $recgametype) {

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

    $sms->send();


}

您还可以创建像 SMSSendingFailedException 这样的自定义异常,然后在 send() 函数中抛出它而不是标准的 \Exception

这将帮助您在控制器中发送适当的响应,例如:

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


}

然后更进一步,如果你在整个项目中都需要它,你可以使用 laravel facade 的概念,并带有一个快速的类别名。

【讨论】:

  • 嘿伙计。感谢回复,这正是我想要的。惊人的,有组织的和工作的。关于我如何在控制器中执行返回还有更多提示吗?因为我正在学习我正在做的事情只是为了工作,如果你发现任何问题,只需提及,我会谷歌它。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-20
  • 2020-11-17
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多