drkenap147751 2017-06-19 18:17
浏览 47
已采纳

Laravel 5.2只是在尝试显示视图时出错

I created a view which allows the user to create a theme/post. The link to that view is in my navbar. But the problem is When I try to click the link (display the view) I get redirected to the home page without any errors whatsoever.

In the link of the navbar, i put <li><a href="{{ route('createtheme') }}"> which simply activates the route with the name "createtheme". This route is as followed: Route::get('/theme/create', 'ThemesController@create')->name('createtheme');. So this activated the create method in the ThemesController.php. Which is:

public function create()
{
    return view('themes.create');
}

So if I read this correctly, This is supposed to give me the right view, right? And it doesn't give me any errors so I don't know where to look.

This is the view I'm trying to display:

@extends('layouts.default')

@section('content')
<div class="container main-content">
    <div class="row first-row">
        <div class="col s12">
            <div class="card">
                <div class="card-content clearfix">
                    <span class="card-title">New theme</span>
                </div>
            </div>
            <div class="card">
                <div class="card-content">
                    <form method="POST" action="">
                        {{ csrf_field() }}
                        <input type="hidden" name="user_id" value="{{ Auth::user()->id }}">
                        <div class="row">
                            <div class="input-field col s6 has-error form-group">
                                <input id="title" class="form-control" type="text" name="title" placeholder="Title of topic">
                                <label for="title" class="active">Title of theme</label><span>Titel is mandatory!</span>
                            </div>
                            <div class="file-field input-field col s6 form-group">
                                <div class="btn cyan darken-1 disabled"><span>Attachment</span>
                                    <input id="attachement" type="file" name="attachment" class="disabled">
                                </div>
                                <div class="file-path-wrapper form-group">
                                    <input type="text" placeholder="geen" class="file-path validate form-control">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col s12 form-group">
                                <textarea id="message-body" class="form-control" name="body"></textarea>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col s6">
                                Hier komen opties om het onderwerp te sluiten of
                                aan te geven dat het opgelost is, alleen bij bewerken
                            </div>
                            <div class="col s6">
                                <a href="" class="btn right cyan darken-1" type="submit">Save</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script type="text/javascript" src="{{ asset('js/ckeditor/ckeditor.js') }}">
</script>
<script type="text/javascript" src="{{ asset('js/editor.js') }}"></script>
<script type="text/javascript" src="{{ asset('js/blog.js') }}"></script>
<script type="text/javascript" src="{{ asset('js/materialize.js') }}">
</script>
@endsection

And here are the routes that are related to this problem.

Route::get('/theme/create', 'ThemesController@create')->name('createtheme');
Route::post('/theme/create', 'ThemesController@store');

And of course the Store method:

public function store(Request $request)
{
    Theme::create($request->input()); //Dit doet hetzelfde als bovenstaande

    return redirect('/');
}

The middleware

public function handle($request, Closure $next)
{

    if ($request->user()->role_id != 2 || 3)
    {
        return redirect('/');
    }

    return $next($request);
}
  • 写回答

2条回答 默认 最新

  • douyu9012 2017-06-19 18:54
    关注

    Problem at that point:

      if ($request->user()->role_id != 2 || 3)
    

    And more specifically in comprasion:

     (Integer value) != 2 || 3
    

    All ingeteger values will return true, even 0. E.g.:

     if (0 != 2 || 3) -> TRUE
     if (2 != 2 || 3) -> TRUE
     if (3 != 2 || 3) -> TRUE
    

    and you get redirect. Change you code at that point for right comprasion ( you use operators not properly). The simplest way:

     $allowed_role_ids = [2,3]; // array(2,3);
     if (!in_array($request->user()->role_id,$allowed_role_ids)){
        // redirect
     } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?