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 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在