1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @Author: ahmadnorin |
5
|
|
|
* @Date: 2017-11-28 00:12:29 |
6
|
|
|
* @Last Modified by: ahmadnorin |
7
|
|
|
* @Last Modified time: 2017-11-28 09:47:54 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace App\Http\Controllers; |
11
|
|
|
|
12
|
|
|
use Illuminate\Http\Request; |
13
|
|
|
use App\ApiManager; |
14
|
|
|
use Validator, Session, Redirect; |
15
|
|
|
|
16
|
|
|
class ApiManagerController extends Controller |
17
|
|
|
{ |
18
|
|
|
public function index(Request $request) |
19
|
|
|
{ |
20
|
|
|
if($request->get('search') != '') |
21
|
|
|
{ |
22
|
|
|
$data['data'] = ApiManager::where('client', 'like', '%'.$request->get('search').'%') |
|
|
|
|
23
|
|
|
->orderBy('id', 'desc') |
24
|
|
|
->paginate(env('PAGINATE', 10)); |
25
|
|
|
|
26
|
|
|
} |
27
|
|
|
else |
28
|
|
|
{ |
29
|
|
|
$data['data'] = ApiManager::orderBy('id', 'desc')->paginate(env('PAGINATE', 10)); |
|
|
|
|
30
|
|
|
|
31
|
|
|
} |
32
|
|
|
return view('api_manager.index', $data); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function create() |
36
|
|
|
{ |
37
|
|
|
return view('api_manager.create'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function store(Request $request) |
41
|
|
|
{ |
42
|
|
|
$validator = Validator::make($request->all(), [ |
43
|
|
|
'client' => 'required|unique:api_manager,client', |
44
|
|
|
'description' => 'required', |
45
|
|
|
]); |
46
|
|
|
if($validator->fails()) |
47
|
|
|
{ |
48
|
|
|
Session::flash('message', 'Please fix the error(s) below'); |
49
|
|
|
return redirect()->back() |
50
|
|
|
->withErrors($validator) |
51
|
|
|
->withInput(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$token = $this->token(); |
55
|
|
|
$api = New ApiManager; |
56
|
|
|
$api->client = str_replace(array('https://', 'http://'), array('',''),$request->input('client')); |
57
|
|
|
$api->api_keys = $token; |
58
|
|
|
$api->api_token = $token; |
59
|
|
|
// $api->api_keys = $this->token(); |
|
|
|
|
60
|
|
|
$api->description = $request->input('description'); |
61
|
|
|
$api->user_id = 1; |
62
|
|
|
$api->save(); |
63
|
|
|
Session::flash('message', 'Api Keys Data Saved Successfuly'); |
64
|
|
|
return Redirect::to('api_manager'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function edit(Request $request, $id) |
68
|
|
|
{ |
69
|
|
|
$data['data'] = ApiManager::findOrFail($id); |
|
|
|
|
70
|
|
|
return view('api_manager.edit', $data); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function update(Request $request, $id) |
74
|
|
|
{ |
75
|
|
|
$validator = Validator::make($request->all(), [ |
76
|
|
|
'client' => 'required|unique:api_manager,client,'.$id, |
77
|
|
|
'description' => 'required', |
78
|
|
|
]); |
79
|
|
|
if($validator->fails()) |
80
|
|
|
{ |
81
|
|
|
Session::flash('message', 'Please fix the error(s) below'); |
82
|
|
|
return redirect()->back() |
83
|
|
|
->withErrors($validator) |
84
|
|
|
->withInput(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$api = ApiManager::findOrFail($id); |
88
|
|
|
$api->client = str_replace(array('https://', 'http://'),array('',''),$request->input('client')); |
89
|
|
|
$api->description = $request->input('description'); |
90
|
|
|
$api->save(); |
91
|
|
|
Session::flash('message', 'Api Keys Data Update Successfuly'); |
92
|
|
|
return Redirect::to('api_manager'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function token() |
96
|
|
|
{ |
97
|
|
|
$length = 100; |
98
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
99
|
|
|
$charactersLength = strlen($characters); |
100
|
|
|
$randomString = ''; |
101
|
|
|
for ($i = 0; $i < $length; $i++) { |
102
|
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
103
|
|
|
} |
104
|
|
|
return $randomString; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.