首先,我显然不完全了解路由。这个问题不是DB相关的,而是管理文件夹中的图像但我使用Resource进程来创建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>
<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>
<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张图片”的链接和“删除整个文件夹”的表单。我假设我在一些教程中找到它并且只是使用它。我想删除一个特定的图像(这可以使用Controller的'edit'组件工作正常)或删除整个文件夹(这也可以使用Controller的'destroy'组件工作正常)。
因此,下一步是能够将新文件夹或图像添加到特定文件夹。使用Controller的“创建”组件添加新文件夹也可以正常工作。
然后它试图添加新图像分崩离析。根据显示的代码,我会得到2个不同的错误,具体取决于我是单击“添加Piccies”按钮还是“添加新图片”按钮。
我的路线是(我已尝试过get和post):
Route::post('/gallery/add/', 'GalleryController@add');
/* Dynamically create all routes for CRUD for gallery table */
Route::resource('gallery', 'GalleryController');
如果我使用'添加piccies'按钮,我会得到一个'http://hdkumdo.com/lsapp/public/gallery/add?2019_Masters'的URL,其中?2019等是相关的文件夹。如果我使用'添加新图片'按钮,我会收到错误“类型错误:函数App \ Http \ Controllers \ GalleryController :: add()的参数太少,0通过,正好是1个预期'。
如果我将路由从post更改为get,则会收到错误'Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message'。
渲染视图如下所示: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);
}
所以,任何建议/建议等都是最受欢迎的。
请参阅上面的评论。这是需要注意的事情,因为我花了好几天头部刮伤,需要一点横向思考。