【问题标题】:Various errors trying to add an extra function to CRUD尝试向 CRUD 添加额外功能的各种错误
【发布时间】:2019-07-31 01:12:20
【问题描述】:

首先,我显然不完全了解路由。这个问题与数据库无关,而是管理文件夹中的图像,但我使用资源进程创建了 crud 元素。也许这不是文件管理的正确方法。

我有如下视图(不是成品图,仅供测试):

@extends('layouts.app')
@section('content')
<!--/*{ { dd(get_defined_vars(), url()->current()) } }*/-->
@if(!Auth::guest())
    @if(Auth::user()->admin == 1)
        <h3>Manage Gallery</h3>
    @else
        <h3>Gallery </h3>   
    @endif
@else
    <h3>Gallery (Visitor)</h3>  
@endif

<style>
.flex-container {
           display: -webkit-flex;
           display: flex;
           -webkit-flex-direction: row;
           flex-direction: row;
           -webkit-align-items: center;
           align-items: center;
           justify-content: space-around;
           flex-wrap: wrap;
           min-width: 100px;
           max-width: 1100px;
        }
        .flex-item {
            display:flex;
        align-items: center;
        /*height: 100px;
        width: 200px;*/
        background-color: #f5f8fa;
        color: white;
        justify-content: space-between;
        margin: 3px;
        padding: 10px 0 0 10px;
        }
.flex-item img{
    flex-grow:0;
    flex-shrink:0;
}
        span {
            padding-left: 5px;
        }
</style>


@if(count($data['folders'])>0)

    <div class="flex-container">
    @foreach ($data['folders'] as $folder)

            <div class="flex-item">

                <form action="{{action('GalleryController@show', $folder)}}" method="get">
                    <input name="_method" type="hidden" value="show">
                        <button class="btn btn-lg px-5 btn-primary" style="margin:5px" type="submit" role="button" <img src="assets/folder_image.png" height="30">{{$folder}}</button>
                </form>

            <!-- --------------------------------------- -->
            <!-- Only show crud buttons if administrator -->
            <!-- --------------------------------------- -->

            @if(!Auth::guest())
                @if(Auth::user()->admin == 1)
                    <a href="{{action('GalleryController@edit',$folder)}}" class="btn btn-primary btn-sm">Delete 1 Picture</a>&nbsp;
                            <form action="{{action('GalleryController@destroy', $folder)}}" method="post">
                                {{csrf_field()}}
                                <input name="_method" type="hidden" value="DELETE">
                                <button class="btn btn-danger btn-sm" type="submit">Delete entire Folder</button>
                            </form>

                            <a href="{{action('GalleryController@add',$folder)}}" class="btn btn-primary btn-sm">Add piccies</a>&nbsp;
                    <form action="{{action('GalleryController@add')}}" method="post">
                                {{csrf_field()}}
                                <input name="folder" type="hidden" value='{{$folder}}'>
                                <button class="btn btn-info btn-sm" style="margin:5px" type="submit">Add new pictures</button>
                            </form>

                @endif
            @endif  


            </div>
    @endforeach
    @if(!Auth::guest())
        @if(Auth::user()->admin == 1)
        <div class="flex-item" style="width:1500px">        <!-- large width forces flexbox to new row -->
            <a href="{{action('GalleryController@create')}}" class="btn btn-primary btn-sm">Create</a>
        </div>  
        @endif
        @endif  

    </div>
@else
    @if(!Auth::guest())
        @if(Auth::user()->admin == 1)
            <br><br><br>
            <a href="{{action('GalleryController@create')}}" class="btn btn-primary btn-sm">Create</a>
        @endif
        @endif  
    <p>No Gallery Folders available.</p>
@endif




@endsection

我不记得为什么我使用“删除 1 张图片”链接和“删除整个文件夹”表单。我假设我在一些教程中找到了它并使用了它。我想删除特定图像(使用控制器的“编辑”组件可以正常工作)或删除整个文件夹(使用控制器的“销毁”组件也可以正常工作)。

因此,下一步是能够将新文件夹或图像添加到特定文件夹。使用控制器的“创建”组件添加新文件夹也可以正常工作。

然后它在尝试添加新图像时崩溃了。使用如图所示的代码,我会收到 2 个不同的错误,具体取决于我是单击“添加图片”按钮还是“添加新图片”按钮。

我的路线是(我已经尝试过get和post):

Route::post('/gallery/add/', 'GalleryController@add');
/* Dynamically create all routes for CRUD for gallery table */
Route::resource('gallery', 'GalleryController');

如果我使用“添加图片”按钮,我会得到一个 URL 为“http://hdkumdo.com/lsapp/public/gallery/add?2019_Masters”,其中 ?2019 等是相关文件夹。 如果我使用“添加新图片”按钮,我会收到错误消息“类型错误:函数 App\Http\Controllers\GalleryController::add() 的参数太少,通过了 0,预期正好是 1”。

如果我将路由从 post 更改为 get,我会收到错误“Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 没有消息'。

渲染的视图如下所示: View of buttons

因为我使用了由 RESOURCE 创建的基本 CRUD 元素,所以我向控制器添加了一个新函数,认为我可以调用它。代码片段是:

/**
     * Show the form for uploading pictures.
     *
     * @return \Illuminate\Http\Response
     */
    public function add($gallery)
    {
    $folder = $gallery;
    $title = 'Add Pictures';
    TheUser::get_Role();
    $userRole = session('userRole');
        Log::info('GalleryController.add called : user Role =>' . $userRole . ' Folder:' . $folder );
    $data = array(
            'userRole' => $userRole,
        );
    /*dd($data);*/
    return view('gallery.add')->with('title', $title)->with('data', $data);
    }

所以,任何建议/建议等都将受到欢迎。

【问题讨论】:

  • 我已经解决了我的问题。基本上,不同的问题是我试图以不同的方式解决潜在问题的结果。首先,我没有清楚地理解我创建的 URL 以及应用的路由。问题的根本原因是我试图传递一个包含文件路径的一部分的参数。 Laravel 以一种看起来像页面路径的方式创建 URL。我最终解决了这个问题并更改了路径字符,因此提供的值被视为正在传递的参数。
  • 这导致了另一个小问题,导致 Laravel 更改了传递的参数。这与字符串中被视为“安全”或“不安全”的字符有关。一旦我将分隔符更改为“安全”值,参数就会按预期传递。

标签: laravel routes crud


【解决方案1】:

参见上面的 cmets。这是需要注意的事情,因为我花了好几天的时间挠头,需要一点横向思考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 2022-12-28
    相关资源
    最近更新 更多