Edit
ユーザーがログインしているときに ボタンが表示される単一のビューをカスタマイズしようとしていDelete
ます。
ユーザーがゲストの場合、これらの 2 つのボタンは非表示になります。
同じコントローラーとビューファイルを使用しようとしています。そして、Auth::check()
すべての管理者専用アイテムの前に、ビュー内で行います。
また、すべてのルートauth
にフィルターを追加しました。Admin
/
問題は、ログに記録されずにアクセスした場合です。期待どおりに機能し、管理者専用のアイテムが非表示になります。管理者としてアクセスすると、管理者専用のアイテムが表示されます。
しかし、Details
アンカータグをクリックすると。/login
管理者のみのルートにアクセスしようとすると、ルートにリダイレクトされます。管理者専用アイテムを非表示にしたいだけです!
詳細ページに到達するために手動で入力しようとしました: /17
ID が に等しい投稿に関する詳細が表示され17
ます。しかし、このタグをクリックすると、にリダイレクトしようとし/admin/post/list/17
ます。私が管理者かどうかに基づいてこれらのリダイレクトを管理する方法は?
編集:無関係なコードを削除しました。
これが私のルートです:
<?php
Route::get('login', array('before' => 'guest' , 'as' => 'getLogin', 'uses' => 'UserController@getLogin'));
Route::post('login', array('before' => 'csrf' , 'as' => 'postLogin', 'uses' => 'UserController@postLogin'));
Route::get('make/me/an/admin/account', array( 'as' => 'getSignup', 'uses' => 'UserController@getCreateAdmin'));
Route::post('make/me/an/admin/account', array('before' => 'csrf', 'as' => 'postSignup', 'uses' => 'UserController@postCreateAdmin'));
Route::get('logout', array('before' => 'auth', 'as' => 'getLogout', 'uses' => 'UserController@getLogout'));
Route::group(array( 'before' => 'auth', 'prefix' => 'admin'), function(){
Route::get('/' , function(){
return View::make('admin.main')->with('title', 'Main');
});
Route::group(array('prefix' => 'post',), function(){
Route::get('/', array('as' => 'listAllPosts', 'uses' => "PostController@listPosts"));
Route::get('list', array('as' => 'listAllPosts', 'uses' => "PostController@listPosts"));
Route::get('list/{id}', array('as' => 'listSinglePost', 'uses' => "PostController@showPost"));
Route::post('addcomment/{post_id}', array('as' => 'addComment', 'uses' => 'CommentController@addComment'));
});
});
});
Route::get('/', array('as' => 'listAllPostsGuest', 'uses' => 'PostController@listPosts'));
Route::get('/{id}', array('as' => 'listSinglePostsGuest', 'uses' => 'PostController@showPost'));
私が話したことを達成しようとしたビュー:
list.blade.php
((foreach 内) にリダイレクトする詳細リンクはどこにありますかlogin
):
@extends('layout.layout')
@section('header')
@stop
@section('content')
@if(Auth::check())
<h2>Main - Admin - Post Main menu</h2>
@else
<h2>Main - Posts</h2>
@endif
@if(Auth::check())
<ul>
<li>{{ link_to_route('getAddPost', 'Add') }}</li>
</ul>
@endif
@if(isset($message))
<p>{{ $message }}</p>
@endif
@if(isset($posts))
<ul>
@foreach($posts as $post)
<li>
<span>{{ $post->body }} - {{ count($post->comments) }} Comment(s)</span>
@if(Auth::check())
{{ Form::open(array('action' => array('PostController@deletePost', $post->id))) }}
{{ Form::submit('delete') }}
{{ Form::close() }}
@endif
{{ link_to_route('PostController@showPost', 'Details', array("id" => $post->id)) }}
</li>
@endforeach
</ul>
@endif
@if(Auth::check())
{{ link_to('admin/', 'Back') }}
@else
{{ link_to('/', 'Back') }}
@endif
@stop
そして、ここに私のコントローラがあります:
<?php
class PostController extends BaseController {
public function listPosts(){
$posts = Post::all();
return View::make('admin.post.list')->with('posts' , $posts);
}
public function showPost($id){
if(!is_numeric(trim($id))){
return Redirect::action('PostController@listPosts');
}
$post = Post::find($id);
if(empty($post)){
return Redirect::action('PostController@listPosts');
}
return View::make('admin.post.postdetails')->with('post', $post);
}
}
?>