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

kazhuyo's avatar

[5.1] fwrite a file with blade form (SOLVED)

Hello everyone... I've created a controller for rewrite services.php in config folder. here's my controller code :

    public function postSocial()
    {   
        
        $rules = array(
                    
        );  
        $validator = Validator::make(Input::all(), $rules); 
        if ($validator->passes()) 
        {
        
        $content = "<?php

return [

    'mailgun' => [
        'domain' => '',
        'secret' => '',
    ],

    'mandrill' => [
        'secret' => '',
    ],

    'ses' => [
        'key'    => '',
        'secret' => '',
        'region' => 'us-east-1',
    ],

    'stripe' => [
        'model'  => 'App\User',
        'secret' => '',
    ],

    'google' => [
        'client_id'     => '',
        'client_secret' => '',
        'redirect'  => '',
    ],

    'twitter' => [
        'client_id'     => '',
        'client_secret' => '',
        'redirect'      => '',
    ],

    'facebook' => [
        'client_id'     => '',
        'client_secret' => '',
        'redirect'      => '',
    ],      

];";
            
            $services = base_path() ."/config/services.php";
                
            $fp=fopen($services,"w+");              
            fwrite($fp,$content); 
            fclose($fp);        
            return Redirect::to('builder/config/social')->with('messagetext','Setting Has Been Save Successful')->with('msgstatus','success');
        }   else {

            return Redirect::to('builder/config/social')->with('messagetext', 'The following errors occurred')->with('msgstatus','error')
            ->withErrors($validator)->withInput();
        }   
    }       

and here's my blade form :

 {!! Form::open(array('url'=>'builder/config/social/', 'class'=>'form-horizontal')) !!}
  <div class="sbox  "> 
    <div class="sbox-title"> {{ Lang::get('core.t_socialmedia') }} </div>
            <div class="sbox-content">      
    <div class="sbox-content">  
    <div class="form-group">
    <label for="ipt" class=" control-label col-sm-4"><i class="fa fa-facebook"></i> Login Facebook </label> 
    <div class="col-sm-8">
            <label for="ipt" class=" control-label ">{{ Lang::get('core.fr_appid') }} </label>
            
            <input type="text" class="form-control" value="" name="FB_ID"  />
            
            <label for="ipt" class=" control-label "> {{ Lang::get('core.fr_secret') }} </label>
            <input type="text" class="form-control" value=""  name="FB_SECRET"  />                          
    </div>  
{!! Form::close() !!}

My problem is, i don't know how to add field in the controller from input button into controller and rewrite the file ?

0 likes
5 replies
jlrdw's avatar

Just post a form and get the data you need and write it, here is an example where I backup transactions to a csv file prior to purging:

public function checkPurge()
    {
        $stmt = $this->db->select("SELECT * FROM checks ORDER BY checkid", array(), \PDO::FETCH_ASSOC);
        $k = 0;
        $numbers[0] = 0;
        $fileout = fopen("cleared.txt", "wb");
        foreach ($stmt as $row) {

            $k = $k + 1;
            if ($k < 2) {
                $checkid = $row['checkid'];
                $transdate = $row['transdate'];
                $transdrscribe = $row['transdescribe'];
                $wd = Cln::fixInt($row['widthdraw']);
                $dep = Cln::fixInt($row['deposit']);
                $isclr = Cln::fixTiny($row['isclr']);
                //$rs = Cln::fixInt($row['runsum']);
                //echo "======k====" . $row['transdescribe'];
            } else {
                if ($row['isclr'] == 1) {
                    $checkid = $row['checkid'];
                    $transdate = $row['transdate'];
                    $transdrscribe = $row['transdescribe'];
                    $wd = Cln::fixInt($row['widthdraw']);
                    $dep = Cln::fixInt($row['deposit']);
                    $isclr = Cln::fixTiny($row['isclr']);
                    //$rs = Cln::fixInt($row['runsum']);
                    //echo "======k====" . $row['transdescribe'];
                } else {
                    //$firstblank = 
                    $k = 100000;
                }
            }
            $newline = $checkid . "     " . $transdate . "                 " . $transdrscribe . "               " . $wd . "     " . $dep . "     " . $isclr . PHP_EOL;

            $postdata = array(
                'checkid' => $checkid,
                'transdate' => $transdate,
                'transdescribe' => $transdrscribe,
                'widthdraw' => $wd,
                'deposit' => $dep,
                'isclr' => $isclr,
            );


            if ($k > 99999)
                break;

            fwrite($fileout, $newline);
            $this->db->insert("checkbacks", $postdata);
             /// more code
1 like
Snapey's avatar
Snapey
Best Answer
Level 122

If you are going to blow away all the other settings, why bother creating empty records? Just create the services you want?

Seems a bit of a sledgehammer approach, surely you could just overwrite the FB values at runtime through the boot method of a service provider?

anyway, how about

$facebook = [
    'client_id' => $request->input('FB_ID'),
    'client_secret' => $request->input('FB_SECRET') 
    ];

$content = "<?php return[ facebook => " . var_export($facebook) . "];";

fwrite($fp,$content); 

(inspiration and props for the var_export suggestion http://heera.it/laravel-tips-update-config-runtime-persistently#.V794Q0IrIo0)

1 like
kazhuyo's avatar

all i want like this :

    'facebook' => [
        'client_id'     => 'data_from_input_field',
        'client_secret' => 'data_from_input_field',
        'redirect'      => 'data_from_input_field',
    ],    

i want to make my customer easy to configure services.php without direct edit the file services.php, all they need just fill out the form and file services.php updated.

any suggestion ?

jlrdw's avatar

You are really overthinking this stuff and not listening to good suggestions. @Snapey gave you a perfect example.

kazhuyo's avatar

Problem Solved...

After search some manual, i've add Input::get into my controller : here's my controller :

    'facebook' => [
        'client_id'     => '".Input::get('FB_ID')."',
        'client_secret' => '".Input::get('FB_SECRET')."',
        'redirect'      => '".Input::get('FB_URL')."',
    ],      

Please or to participate in this conversation.