Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

manojvarma's avatar

sending data from one controller to another controller

HI, i am sending messages to customers from an api. so i created a trait. but in message i need to send dynamic data , i dont know how to take that message from controller and to pass trait. here is my controller code.

public function add()
    {
        $test1 = device::where('id', Input::get('id'))->firstOrFail();
          $test1->courier = Input::get('courier');
          $test1->track = Input::get('track');
          $test1->status = '4';
          $test1->save();
          $test1 = $this->sendsms();
        Alert::success('Data successfully stored');
        return redirect('readytoship');
        
    }

from the above controller i need to take courier value and track value and i want to send that values to sendsms method in SmsTrait. my SmsTrait code is

trait SmsTrait {
    public function sendsms()
    {
    // Account details
    $username = '********************';
    $hash = '**********************************';
    
    // Message details
    $numbers = array(*************);
    $sender = urlencode('**************');
    $message = rawurlencode('My message ');
 
    $numbers = implode(',', $numbers);
 
    // Prepare data for POST request
    $data = array('username' => $username, 'hash' => $hash, 'numbers' => $numbers, "sender" => $sender, "message" => $message);
    $ch = curl_init();
    // Send the POST request with cURL
    $ch = curl_init('http://api.textlocal.in/send/');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    // Process your response here
    echo $response;
}
}

In $message i want to send the courier and track details

0 likes
12 replies
larsonator's avatar

you could pass the values through as parameters.

$this->sendsms($test1->courier);
---------------------------------------------------
public function sendsms($message='')
    {
.
.
.

Id probably even look into extracting this into its own class.

willvincent's avatar

What version of laravel? If you're using 5.3, why not just use the notifications subsystem?

If you're not using 5.3, the best way to handle this probably would be to fire an event, and handle the message sending with a listener.

willvincent's avatar

Yeah, in that case, use the notifications stuff, that's what it's there for.

willvincent's avatar

@manojvarma build a custom notifications channel around that code, and modify as necessary. Read the docs I linked to previously.. it should tell you everything you need to know to build a custom channel to use your desired service.

https://laravel.com/docs/5.3/notifications#custom-channels

You seem resistant to this approach and I really don't understand why.. it's the right way to accomplish this.

martinbean's avatar

@manojvarma You don’t want to be doing that from your controller. You should at least be queuing it so users are waiting for your application to respond whilst you app goes off to an SMS gateway and waits for that to respond.

As others above have suggested, use notifications. Yes, it only has a Nexmo API out of the box, but you can create a custom channel for the SMS gateway you’re using instead. Then, you can just notify users when something happens instead of pulling in your trait in every controller. When you’re dropping code around in multiple places like that, it’s a sign you need to re-factor and that there’s probably a better approach.

class OrderController extends Controller
{
    public function ship(Request $request, Order $order)
    {
        $shipment = $order->ship($request->courier);

        $order->user->notify(new OrderShipped($shipment));

        return redirect()->route('order.show', $order)->withSuccess('Order shipped.');
    }
}
manojvarma's avatar

Hi, I dont know where to start to send message using textlocal. People suggested to use custom notification channel. But for textlocal there is no specific installation method like nexmo, twilio and all. Now my doubt is how to install textlocal as a notification client?

Please or to participate in this conversation.