dptiq46022 2013-09-06 21:47
浏览 28
已采纳

Laravel4 - 在以访客身份进入时隐藏某些功能的视图

I'm trying to customize a single view that when the user is logged in, It will show Edit and Delete buttons.

When the user is a guest, Those two buttons are going to be hidden.

I'm trying to use the same controllers and view files. And do the Auth::check() inside the views before every admin-only item.

Also i've added auth filter in all of the Admin routes.

The problem is when i access / without being logged it. It works as expected and hides the admin-only items. When accessing it as an admin it shows the admin-only items.

But when i click Details anchor tag. I get redirected to /login route as i'm trying to access an admin only route. I just want it to hide the admin-only items !

I've tried to type it manually to reach the details page : /17 it gets me details about post with id equals to 17. But when i click this tag it tries to redirects me to /admin/post/list/17. How to manage these redirection based on if i'm an admin or not ?

EDIT: I've deleted unrelated code.

Here's my routes :

<?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'));

The views that i tried to achieve what i talked about :

list.blade.php (Where is the details link that redirects me to login (inside the foreach) ):

@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

And here's my Controllers :

<?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);
    }
}
?>
  • 写回答

1条回答 默认 最新

  • dongtui9168 2013-09-07 00:32
    关注

    Every URI that begins with admin/ will have the auth filter run prior to any controller code loading. If your auth filter does any redirects, that's where the issue lies.

    I'd recommend really figuring out how your URI structure is going to be set up (having public-facing URIs that start with admin doesn't make much sense). After that, you can set up your routes more efficiently and with less filter conflicts and such.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)