Sent All Variables in Controller Function to the View

- 1 answer

Ad

I have a function in my controller. I created a lot of variables, and send them to my view via compact function one by one.


public function edit($id,$cpe_mac) {

        $vcpe = VSE::vcpe($cpe_mac);
        $vcpe = json_decode (json_encode($vcpe), FALSE);
        $cpe = VSE::cpe($cpe_mac);
        $wan = $cpe['wan'];
        $acl = $cpe['acl'];
        $guest = $cpe['vlan'][0];
        $private = $cpe['vlan'][1];
        $cpe_name = VSE::cpe_name($cpe_mac)['cpe_name'];
        $p_max_up = $private['bandwidth']['max_up'];
        $p_max_down = $private['bandwidth']['max_down'];
        $p_ip = $private['lan']['ip_address'];
        $p_netmask = $private['lan']['netmask'];
        $p_max_clients = $private['lan']['dhcp_server']['max_clients'];
        $p_dns = $private['lan']['dhcp_server']['dns'][0];
        $p_dns2 = $private['lan']['dhcp_server']['dns'][1];
        $cpe = json_decode (json_encode($cpe), FALSE);

        return view('cpe.edit', compact(['vcpe','cpe_mac','cpe_name','cpe','wan','acl','private','guest',
            'p_max_up','p_max_down','p_ip','p_netmask','p_max_clients','p_dns','p_dns2'

        ]));

}

Question

Is there a way to send all the variables to the view rather than doing it one by one ?

Ad

Answer

Ad

You could do the following...

return view('cpe.edit', get_defined_vars());

There might be some unnecessary overhead though if you are creating a lot of variables which you would not otherwise need in your view.

Ad
source: stackoverflow.com
Ad