-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
RecipesController.php
138 lines (117 loc) · 3.32 KB
/
RecipesController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Repositories\Recipe\RecipeInterface;
use App\Services\Form\Recipe\RecipeForm;
use App\Models\Recipe;
class RecipesController extends Controller
{
protected $recipe;
protected $recipeForm;
/**
* Create a new controller instance.
*
* @param \App\Repositories\Recipe\RecipeInterface $recipe
* @param \App\Services\Form\Recipe\RecipeForm $recipeForm
* @return void
*/
public function __construct(RecipeInterface $recipe, RecipeForm $recipeForm)
{
$this->middleware('auth');
$this->middleware('acl');
$this->recipe = $recipe;
$this->recipeForm = $recipeForm;
}
/**
* Display a listing of the resource.
*
* @param \Illuminate\Http\Request $request
* @return Response
*/
public function index(Request $request)
{
$page = $request->input('page', 1);
$perPage = 10;
$recipes = $this->recipe->byPage($page, $perPage);
return view('recipes.index')->with('recipes', $recipes);
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
return view('recipes.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return Response
*/
public function store(Request $request)
{
$input = $request->all();
if ($this->recipeForm->save($input)) {
return redirect()->route('recipes.index');
} else {
return redirect()->route('recipes.create')
->withInput()
->withErrors($this->recipeForm->errors());
}
}
/**
* Display the specified resource.
*
* @param \App\Models\Recipe $recipe
* @return Response
*/
public function show(Recipe $recipe)
{
$recipeProject = $recipe->getProjects()->toArray();
return view('recipes.show')->with('recipe', $recipe)
->with('recipeProject', $recipeProject);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Recipe $recipe
* @return Response
*/
public function edit(Recipe $recipe)
{
return view('recipes.edit')->with('recipe', $recipe);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Recipe $recipe
* @return Response
*/
public function update(Request $request, Recipe $recipe)
{
$input = array_merge($request->all(), ['id' => $recipe->id]);
if ($this->recipeForm->update($input)) {
return redirect()->route('recipes.index');
} else {
return redirect()->route('recipes.edit', [$recipe])
->withInput()
->withErrors($this->recipeForm->errors());
}
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Recipe $recipe
* @return Response
*/
public function destroy(Recipe $recipe)
{
$this->recipe->delete($recipe->id);
return redirect()->route('recipes.index');
}
}