Skip to content

Commit be44489

Browse files
committed
Added: 1.List of posts; 2.Create post Page
1 parent 244e1ef commit be44489

File tree

20 files changed

+719
-303
lines changed

20 files changed

+719
-303
lines changed

.idea/workspace.xml

Lines changed: 308 additions & 283 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/Http/Controllers/AdminController.php

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
namespace App\Http\Controllers;
44

55
use App\Http\Requests\EditUserRequest;
6+
use App\Http\Requests\PostCreateRequest;
67
use App\Http\Requests\UsersRequest;
78
use App\Photo;
9+
use App\Post;
810
use App\Role;
911
use App\User;
1012
use Illuminate\Http\Request;
1113

1214
use App\Http\Requests;
15+
use Illuminate\Support\Facades\Auth;
1316
use Illuminate\Support\Facades\Hash;
17+
use Illuminate\Support\Facades\Session;
1418

1519
class AdminController extends Controller
1620
{
@@ -146,6 +150,10 @@ public function updateUser(EditUserRequest $request, $id){
146150
//получение указателя на файл загруженный пользователем
147151
$file = $request->file('path');
148152

153+
//сохранения данных об операции в сессии
154+
Session::flash('data_state', 'has been updated');
155+
Session::flash('user_name', $user->name);
156+
149157
//сохраненние отредактированных данных пользователя
150158
$user->name = $request->name;
151159
$user->email= $request->email;
@@ -161,8 +169,8 @@ public function updateUser(EditUserRequest $request, $id){
161169
//удаление сущестующей аватарки пользователя
162170
if($user->photo_id){
163171
if($user->photo){
164-
if(file_exists($user->photo->path)){
165-
unlink($user->photo->path);
172+
if(file_exists(public_path().$user->photo->path)){
173+
unlink(public_path().$user->photo->path);
166174
}
167175
}
168176
}
@@ -186,4 +194,76 @@ public function updateUser(EditUserRequest $request, $id){
186194

187195
return redirect('admin/users');
188196
}
197+
198+
public function destroyUser($id){
199+
//поиск пользователя в БД
200+
$user = User::findOrFail($id);
201+
202+
//сохранение данных в сессию для их вывода в инф.сообщении
203+
Session::flash('data_state','Has been deleted');
204+
Session::flash('user_name',$user->name);
205+
206+
//удаление существующей аватарки пользователя
207+
if($user->photo_id){
208+
if($user->photo){
209+
if(file_exists(public_path().$user->photo->path)){
210+
unlink(public_path().$user->photo->path);
211+
}
212+
}
213+
}
214+
215+
$user->delete();
216+
return redirect('admin/users');
217+
}
218+
219+
public function posts(){
220+
$posts = Post::all();
221+
return view('admin/posts/index', compact('posts'));
222+
}
223+
224+
public function createPost(){
225+
// $categories = Category::lists('name', 'id');
226+
//
227+
return view('admin/posts/create'/*, compact('categories')*/);
228+
}
229+
230+
public function storePost(PostCreateRequest $request){
231+
$user = Auth::user();
232+
$file = $request->hasFile('photo_id') ? $request->file('photo_id') : null;
233+
234+
if($user){
235+
if($request->hasFile('photo_id')){
236+
$post = new Post();
237+
$post->user_id = $user->id;
238+
$post->category_id = $request->input('category_id');
239+
$post->title = $request->input('title');
240+
$post->body = $request->input('body');
241+
242+
if($file){
243+
$photo = new Photo();
244+
$photo->path = time().$file->getClientOriginalName();
245+
246+
$file->move('image', $photo->path);
247+
248+
$photo->save();
249+
250+
$post->photo_id = $photo->id;
251+
}
252+
253+
$post->save();
254+
}
255+
256+
return redirect('admin/posts');
257+
}
258+
259+
return $request->all();
260+
}
261+
262+
public function editPost($id){
263+
264+
}
265+
266+
public function updatePost(Request $request, $id){
267+
268+
}
189269
}

app/Http/Controllers/Auth/AuthController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class AuthController extends Controller
2828
*
2929
* @var string
3030
*/
31-
protected $redirectTo = '/';
31+
protected $redirectTo = '/admin';
3232

3333
/**
3434
* Create a new authentication controller instance.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
use App\Http\Requests;
8+
9+
class PostController extends Controller
10+
{
11+
//
12+
}

app/Http/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ class Kernel extends HttpKernel
4949
'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
5050
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
5151
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
52+
'admin' => \App\Http\Middleware\Admin::class,
5253
];
5354
}

app/Http/Middleware/Admin.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Middleware;
44

55
use Closure;
6+
use Illuminate\Support\Facades\Auth;
67

78
class Admin
89
{
@@ -15,6 +16,12 @@ class Admin
1516
*/
1617
public function handle($request, Closure $next)
1718
{
18-
return $next($request);
19+
if(Auth::check()){ /*залогинен ли пользователь?*/
20+
if(Auth::user()->isAdmin()){ /*пользователь администратор?*/
21+
return $next($request);
22+
}
23+
}
24+
25+
return redirect('/');
1926
}
2027
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use App\Http\Requests\Request;
6+
7+
class PostCreateRequest extends Request
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*
12+
* @return bool
13+
*/
14+
public function authorize()
15+
{
16+
return true;
17+
}
18+
19+
/**
20+
* Get the validation rules that apply to the request.
21+
*
22+
* @return array
23+
*/
24+
public function rules()
25+
{
26+
return [
27+
'category_id' =>'required',
28+
'photo_id' =>'required',
29+
'title' =>'required',
30+
'body' =>'required'
31+
];
32+
}
33+
}

app/Http/routes.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,24 @@
1919

2020
Route::get('/home', 'HomeController@index');
2121

22-
Route::get('/admin/users', 'AdminController@users');
2322

24-
Route::get('/admin/users/create', 'AdminController@createUser');
2523

26-
Route::post('/admin/users/storeuser', 'AdminController@storeUser');
27-
28-
Route::get('/admin/users/edit/{id}', 'AdminController@editUser');
24+
Route::group(['middleware' => 'admin'], function(){
25+
26+
Route::get ('/admin/users' , 'AdminController@users' );
27+
Route::get ('/admin/users/create' , 'AdminController@createUser' );
28+
Route::post ('/admin/users/storeuser' , 'AdminController@storeUser' );
29+
Route::get ('/admin/users/edit/{id}' , 'AdminController@editUser' );
30+
Route::post ('/admin/users/update/{id}' , 'AdminController@updateUser' );
31+
Route::delete ('/admin/users/destroy/{id}', 'AdminController@destroyUser' );
32+
Route::get ('admin/posts' , 'AdminController@posts' );
33+
Route::get ('admin/posts/create' , 'AdminController@createPost' );
34+
Route::post ('admin/posts/store' , 'AdminController@storePost' );
35+
Route::get ('admin/posts/edit/{id}' , 'AdminController@editPost' );
36+
Route::post ('admin/posts/update/{id}' , 'AdminController@updatePost' );
37+
Route::delete ('admin/posts/delete/{id}' , 'AdminController@deletePost' );
38+
});
2939

40+
Route::get ('/admin' , function(){ return view('admin.index'); } );
3041

31-
Route::post('/admin/users/update/{id}', 'AdminController@updateUser');
3242

33-
Route::get('/admin', function(){
34-
return view('admin.index');
35-
});

app/Post.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Post extends Model
8+
{
9+
//
10+
public function user(){
11+
return $this->belongsTo('App\User', 'user_id', 'id');
12+
}
13+
14+
public function photo(){
15+
return $this->belongsTo('App\Photo','photo_id','id');
16+
}
17+
18+
public function category(){
19+
return $this->belongsTo('App\Category', 'category_id','id');
20+
}
21+
}

app/User.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,29 @@ class User extends Authenticatable
2424
'password', 'remember_token',
2525
];
2626

27-
//Получение списка ролей выбранного пользователя
27+
//Получение списка ролей пользователя
2828
public function roles(){
2929
return $this->belongsTo('App\Role', 'role_id', 'id');
3030
}
3131

32+
//Получение фотографии пользователя
3233
public function photo(){
3334
return $this->belongsTo('App\Photo', 'photo_id', 'id');
3435
}
36+
37+
//Получение списка постов пользователя
38+
public function posts(){
39+
return $this->hasMany('App\Post', 'user_id', 'id');
40+
}
41+
42+
//Является ли пользователь администратором
43+
public function isAdmin(){
44+
$isAdmin = false;
45+
46+
if($this->roles->name == "administrator" && $this->is_active == 1) {
47+
$isAdmin = true;
48+
}
49+
50+
return $isAdmin;
51+
}
3552
}

0 commit comments

Comments
 (0)