如何添加我的功能并在控制器中正确使用它?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何添加我的功能并在控制器中正确使用它?相关的知识,希望对你有一定的参考价值。

我有'发送'功能,我在我的一个控制器中使用它并且工作正常。现在我需要知道如何让这个代码的类引用在其他控制器中使用它,而不是在所有控制器中复制/粘贴整个代码。在其他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;
    



我现在正在学习,我是初学者,所以帮助理解如何让它正常工作。谢谢。

答案

您可以创建一个单独的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,并在\Exception函数内抛出它而不是标准send()

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

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的概念,如果你需要在整个项目中使用快速类别名。

以上是关于如何添加我的功能并在控制器中正确使用它?的主要内容,如果未能解决你的问题,请参考以下文章

iOS - 如何推送到视图控制器并在返回时保持正确的“视图堆栈”

Laravel - 如何将参数从控制器传递到路由并在另一个控制器中使用它?

如何在 chrome 控制台中保留该功能并在重新加载页面后运行它?

如何获取用户选择的按钮值并在 laravel 5.4 的控制器上使用它

如何在我的应用程序中使用 Facebook 登录并在成功登录后移至下一页/视图控制器?

状态恢复iOS时播放声音