return redirect()->back()->with(['message'=>'У Вас нет прав']);
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
class AdminPostController extends Controller
{
public function show()
{
return view('default.add_post',['title' => 'Новый материал']);
}
public function create(Request $request) {
if(\Gate::denies('add-article')) {
return redirect()->back()->with(['message'=>'У Вас нет прав']);
}
$this->validate($request,[
'name'=>'required'
]);
$user = \Auth::user();
$data = $request->all();
$res = $user->articles()->create([
'name' => $data['name'],
'img' => $data['img'],
'text' => $data['text']
]);
return redirect()->back()->with('message','Материал добавлен');
}
}
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Article;
class AdminUpdatePostController extends Controller
{
public function show(Request $request, $id) {
$article = Article::find($id);
return view('default.update_post',['title' => 'Редактирование материала','article' => $article]);
}
public function create(Request $request) {
$this->validate($request,[
'name'=>'required'
]);
$user = \Auth::user();
$data = $request->except('_token');
$article = Article::find($data['id']);
if(\Gate::allows('update-article',$article)) {
$article->name = $data['name'];
$article->img = $data['img'];
$article->text = $data['text'];
$res = $user->articles()->save($article);
return redirect()->back()->with('message','Материал обновлен');
}
return redirect()->back()->with(['message'=>'У Вас нет прав']);
}
}
namespace App\Providers;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use App\User;
class AuthServiceProvider extends ServiceProvider
{
public function boot(GateContract $gate)
{
$this->registerPolicies($gate);
//$gate->define('add-article',ClassName@func);
$gate->define('add-article',function(User $user) {
foreach($user->roles as $role) {
if($role->name == 'Admin') {
return TRUE;
}
}
return FALSE;//FALSE
});
$gate->define('update-article', function (User $user, $article) {
foreach($user->roles as $role) {
if($role->name == 'Admin') {
if($user->id == $article->user_id) {
return TRUE;
}
}
}
return FALSE;
});
}
}
Route::group(['middleware' => 'web'], function () {
Route::auth();
});
//Route::auth();
// admin/edit/post
Route::group(['prefix'=>'admin','middleware'=>['web','auth']],function() {
// admin/
Route::get('/',['uses'=>'Admin\AdminController@show','as'=>'admin_index']);
Route::get('/add/post',['uses'=>'Admin\AdminPostController@show','as'=>'admin_add_post']);
Route::post('/add/post',['uses'=>'Admin\AdminPostController@create','as'=>'admin_add_post_p']);
Route::get('/update/post/{id}',['uses'=>'Admin\AdminUpdatePostController@show','as'=>'admin_add_post']);
Route::post('/update/post',['uses'=>'Admin\AdminUpdatePostController@create','as'=>'admin_update_post_p']);
});
@extends('default.layouts.layout')
@section('content')
<div class="col-md-9">
<div class="">
<h2>Добавление нового материала</h2>
</div>
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if (session('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
@endif
<form method="post" action="{{ route('admin_add_post_p') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="name">Заголовок</label>
<input type="text" class="form-control" id="name" name="name" value="{{ old('name') }}" placeholder="Заголовок">
</div>
<div class="form-group">
<label for="img">Изображение</label>
<input type="text" class="form-control" id="img" value="{{ old('img') }}" name="img" placeholder="img">
</div>
<div class="form-group">
<label for="text">Text</label>
<textarea class="form-control" id="text" name="text" rows="3">{{ old('text') }}</textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
@endsection
@extends('default.layouts.layout')
@section('content')
<div class="col-md-9">
<div class="">
<h2>Редактирование материала</h2>
</div>
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if (session('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
@endif
<form method="post" action="{{ route('admin_update_post_p') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $article->id }}">
<div class="form-group">
<label for="name">Заголовок</label>
<input type="text" class="form-control" id="name" name="name" value="{{ $article->name }}" placeholder="Заголовок">
</div>
<div class="form-group">
<label for="img">Изображение</label>
<input type="text" class="form-control" id="img" value="{{ $article->img }}" name="img" placeholder="img">
</div>
<div class="form-group">
<label for="text">Text</label>
<textarea class="form-control" id="text" name="text" rows="3">{{ $article->text }}</textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
@endsection
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
// \Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\View\Middleware\ShareErrorsFromSession::class
//\App\Http\Middleware\Mymiddleware::class
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
],
'api' => [
'throttle:60,1',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'mymiddle'=> \App\Http\Middleware\Mymiddleware::class
];
}
но редиректа и сообщений о добавлении, обновлений нет
Если зайти под пользователем без права доступа. Вместо этого по роуту site/admin/add/post/17:
"Sorry, the page you are looking for could not be found."
1/1 NotFoundHttpException in RouteCollection.php line 161