0

Editユーザーがログインしているときに ボタンが表示される単一のビューをカスタマイズしようとしていDeleteます。

ユーザーがゲストの場合、これらの 2 つのボタンは非表示になります。

同じコントローラーとビューファイルを使用しようとしています。そして、Auth::check()すべての管理者専用アイテムの前に、ビュー内で行います。

また、すべてのルートauthにフィルターを追加しました。Admin

/問題は、ログに記録されずにアクセスした場合です。期待どおりに機能し、管理者専用のアイテムが非表示になります。管理者としてアクセスすると、管理者専用のアイテムが表示されます。

しかし、Detailsアンカータグをクリックすると。/login管理者のみのルートにアクセスしようとすると、ルートにリダイレクトされます。管理者専用アイテムを非表示にしたいだけです!

詳細ページに到達するために手動で入力しようとしました: /17ID が に等しい投稿に関する詳細が表示され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);
    }
}
?>
4

1 に答える 1

1

で始まるすべての URIでは、コントローラー コードが読み込まれる前にフィルターが実行されますadmin/auth認証フィルターがリダイレクトを行う場合、そこに問題があります。

URI 構造がどのように設定されるかを実際に把握することをお勧めします (で始まる公開 URI を持つadminことはあまり意味がありません)。その後、ルートをより効率的に設定し、フィルターの競合などを減らすことができます。

于 2013-09-07T00:32:44.933 に答える