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 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路