如何缩进Laravel控制器,查看代码?

时间:2017-04-20 12:43:58

标签: php excel laravel indentation

我的一个项目需要代码缩进,它有许多控制器文件看起来像下面的代码。

基本上我想做的是格式代码,应该自动完成而不需要手动更改每个文件。

控制器代码

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\URL;
use File;
use Html;
use DB;
use Illuminate\Validation\Rule;
use Illuminate\Support\Str;
use Validator;
use Datatables;
use AppHelper;
use LaraCore;
use Image;
use App\Models\Customer;
use App\Models\_List;
use Carbon;



class CustomerController extends Controller
{

public function validator(array $data, $id = NULL)
{
    $email_Rules = explode(',',"required,email,unique");
    $email_Rules['unique'] = Rule::unique('customers')->ignore($id);
    if(($key = array_search('unique',$email_Rules)) !== false) {
      unset($email_Rules[$key]);
    }
                                                                                                                                                                                                                                                                                            return Validator::make($data, [
                                          'secret_key' => 'required|min:6|max:10', 
                                                                'email' => $email_Rules, 
                                                                                                                            'first_name' => 'required|alpha_num', 
                                                                'city' => 'required', 

        ]            
        );

}
/**
 * Display a listing of the resource.
 *
 * @return  \Illuminate\Http\Response
 */
public function index()
{


    return view('panel.customers.index');
}

public function create()
{
    $data = array();
                                                              $EmployeeType = _List::where('list_name','=','EmployeeType')->pluck('item_name','id')->all();
        $data['EmployeeType'] = $EmployeeType;
                                                      $Category = _List::where('list_name','=','Category')->pluck('item_name','id')->all();
        $data['Category'] = $Category;
                                                                                                                                                                                                                            return view('panel.customers.create',$data);
}

public function edit($id,Request $request)
{
    $data = array();
    $Dates = array();
    $customers = Customer::findOrFail($id);
    $data['customers'] = $customers;



                            $EmployeeType = _List::where('list_name','=','EmployeeType')->pluck('item_name','id')->all();
        $data['EmployeeType'] = $EmployeeType;

                                      $preference= explode(',',$customers->preference);        
        $customers->preference = $preference;

                            $Category = _List::where('list_name','=','Category')->pluck('item_name','id')->all();
        $data['Category'] = $Category;




                  if($customers->ready!=''){
          $ready= explode(',',$customers->ready);        
          $customers->ready = $ready;
        }

                  if($customers->approved!=''){
          $approved= explode(',',$customers->approved);        
          $customers->approved = $approved;
        }





                                                $Name = explode(',','start_date,end_date');
        $Dates[0] = new Carbon($customers->$Name[0]);
        $Dates[1] = new Carbon($customers->$Name[1]);
        $data['start_dateend_date'] = $Dates[0]->format('m/d/Y').' - '.$Dates[1]->format('m/d/Y') ;

                                                    return view('panel.customers.create',$data);        
}

public function store(Request $request,$id="")
{
  $this->validator($request->all(),$request->id)->validate();
  if($request->id == null || $request->id == ""){
  $inputs = $request->all();

                               if($request->has('secret_key')){
         $secret_key = bcrypt($request->secret_key);
         $inputs['secret_key'] = $secret_key;
      }



                  if($request->has('preference')){
        $preference = implode(',',$request->preference);
        $inputs['preference'] = $preference;
      }







      if($request->has('ready')){
        $ready = implode(',',$request->ready);
        $inputs['ready'] = $ready;
      }else{
        $inputs['ready'] = "";
      }







                         if($request->has('start_date,end_date')){
          $Dates = explode('-',$request->input('start_date,end_date'));
          $Name = explode(',','start_date,end_date');
          $inputs[''.$Name[0]] = $Dates[0];
          $Dates[0] = new Carbon($Dates[0]);
          $Dates[1] = new Carbon($Dates[1]);
          $inputs[''.$Name[0]] = $Dates[0]->format('Y-m-d');
          $inputs[''.$Name[1]] = $Dates[1]->format('Y-m-d');
          //dd($inputs);
        }  


  foreach($request->files as $key_file => $value_file){
    $nameArr = explode(',',$key_file);
    $file = $value_file;
    $destinationPath = 'public/uploads/customers';
    \File::makeDirectory($destinationPath,0775,true,true);
    $name =  $file->getClientOriginalName();
    $extension = $file->getClientOriginalExtension();
    $encrypted_name = md5(uniqid().time()).".".$extension;
    $file->move($destinationPath,$encrypted_name);
    $inputs[''.$nameArr[0]] = $name;
    $inputs[''.$nameArr[1]] = $encrypted_name;
  }

  Customer::create($inputs);   
  return redirect()->route('customers.index')->with('success',"Customer Saved successfully");     
  }
  else{
    $customers = Customer::where(['id'=>$request->input('id')])->first();
    $inputs = $request->except('_token');

                                    if($request->has('secret_key')){
         $secret_key = bcrypt($request->secret_key);
         $inputs['secret_key'] = $secret_key;
      }



                      if($request->has('preference')){
        $preference = implode(',',$request->preference);
        $inputs['preference'] = $preference;
      }







        if($request->has('ready')){
          $ready = implode(',',$request->ready);
          $inputs['ready'] = $ready;
        }        







                              if($request->has('start_date,end_date')){
          $Dates = explode('-',$request->input('start_date,end_date'));
          $Name = explode(',','start_date,end_date');
          $inputs[''.$Name[0]] = $Dates[0];
          $Dates[0] = new Carbon($Dates[0]);
          $Dates[1] = new Carbon($Dates[1]);
          $inputs[''.$Name[0]] = $Dates[0]->format('Y-m-d');
          $inputs[''.$Name[1]] = $Dates[1]->format('Y-m-d');
          //dd($inputs);
        }  

                                          foreach($request->files as $key_file => $value_file){
      $destinationPath = 'public/uploads/customers';
      \File::makeDirectory($destinationPath,0775,true,true);
      $nameArr = explode(',',$key_file);
      $OldFile = $customers->$nameArr[1];
      if($OldFile!=''){
        $OldFile = $destinationPath.'/'.$OldFile;
        if(\File::exists($OldFile)){
          \File::delete($OldFile);
        }
      }
      $file = $value_file;
      $name =  $file->getClientOriginalName();
      $extension = $file->getClientOriginalExtension();
      $encrypted_name = md5(uniqid().time()).".".$extension;
      $file->move($destinationPath,$encrypted_name);
      $inputs[''.$nameArr[0]] = $name;
      $inputs[''.$nameArr[1]] = $encrypted_name;
      $SavedFile = $destinationPath.$encrypted_name;
    }
    $customers->update($inputs);
    return redirect()->route('customers.index')->with('success',"Customer Saved successfully");
  }

}

public function listCustomer(Request $request)
{
  $customers  = Customer::all();      

  $datatables =  Datatables::of($customers)
  ->addColumn('actions', function($customers){
        $html = '';  

            $html .= '<a href="'.route('customers.edit', ['customer' => $customers->id]).'" class="btn show-tooltip btn-action" name="" data-id="'.$customers->id.'" title="Edit"><i class="fa fa-edit"></i></a>';

            $html .= '<a href="javascript:void(0)" data-toggle="modal" data-target="#delete_customer_modal" class="btn show-tooltip btn-action" name="" data-id="'.$customers->id.'" data-action="'.route('customers.destroy',  ['customer' => $customers->id]).'" title="Delete"><i class="fa fa-trash"></i></a>';
        return $html;
    })
  ->setRowId('id');

  return $datatables->make();
}

public function destroy($id)
{
    $customer = Customer::find($id);
    $customer->delete();     

    return response()->json(array(
        "status"  =>  "success",
        "message" =>  "Customer Deleted Successfully",
        ));   
} 
   }

2 个答案:

答案 0 :(得分:2)

也许你可以在你创建的文件上调用php美化器? http://pear.php.net/package/PHP_Beautifier/

注意:您可以使用exec

调用命令
exec("php_beautifier $filename $filename");

答案 1 :(得分:0)

如果没有更多的来源,很难分辨,但我想像@foreach($fields as $key => $value) @if($value['dbfield'] == '') @continue @endif @if($value['validation']) @if(in_array('unique',explode(',',$value['validation']))) '{{$value['dbfield']}}' => ${{$value['dbfield']}}_rules, @else @if(strpos($value["dbfield"],",")) '{{ explode(',', $value["dbfield"] )[0] }}' => '{{str_replace(',', '|', $value['validation'])}}', @else '{{$value['dbfield']}}' => '{{str_replace(',', '|', $value['validation'])}}', @endif @endif @endif @endforeach 这样的表达式会捕捉某种制表符或导入空格字段,这些字段会以某种方式转换为空格或制表符,从而导致不需要的间距。

{{1}}