子视图刀片中的 Laravel 表单无法发布上传
Posted
技术标签:
【中文标题】子视图刀片中的 Laravel 表单无法发布上传【英文标题】:Laravel form in subview blade fails to POST upload 【发布时间】:2020-11-06 17:59:00 【问题描述】:我是 laravel 新手,无法找到解决此问题的方法。我在 Laravel 视图中有一个名为 create.blade.php 的文件选择器模式,用于上传文件。此子视图在名为 edit.blade.php 的刀片中用于编辑页面。我的表单返回一条成功消息,但在进行一些日志记录后,我可以看到它没有通过 POST 请求访问 AttachmentController@Store。尝试修改路由时,出现“此路由不支持 POST 方法”的错误。
路由 - routes/web.php
//templates
Route::get('/company/company_id/templates/template_id/rev_id', 'CompanyController@template_edit');
//attachments
Route::get('/company/company_id/templates/template_id/attachment','AttachmentController@create');
Route::post('/company/company_id/templates/template_id/attachment','AttachmentController@store');
控制器 - app/Http/Controllers/AttachmentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use File;
use Illuminate\Support\Facades\Storage;
use App\Attachment;
class AttachmentController extends Controller
//begin store
public function store(Request $request,$company_id,$template_id)
$this->validate($request, [
'filenames' => 'required|max:100000'
]);
if($request->hasfile('filenames'))
foreach($request->file('filenames') as $file)
$name = time().'.'.$file->extension();
Storage::disk('local')->putFileAs('attachment', $file, $name);
$originalname = $file->getClientOriginalName();
$path = storage_path('app/attachment/'.$name);
//begin record
$attachment = new Attachment();
$attachment->name = $name;
$attachment->originalname = $originalname;
$attachment->filepath = $path;
$attachment->company_id = $company->id;
$attachment->template_id = $template->id;
$attachment->rev_id = $rev->id;
$attachment->save();
//end record
return back()->with('success', 'Your files have been successfully added.');
//end store
//begin create
public function create()
return view('company.templates.attachment.create');
//end create
查看 - 资源/视图/公司/模板/edit.blade.php
<!-- Begin Attachment Error / Success Pane -->
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Sorry!</strong> There were problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li> $error </li>
@endforeach
</ul>
</div>
@endif
@if(session('success'))
<div class="alert alert-success">
session('success')
</div>
@endif
<!-- End Attachment Error / Success Pane -->
<!-- Begin Attachment Modal -->
<button type="button" class="btn btn-primary float-right ml-2 mr-2" data-toggle="modal" data-target="#uploadModal">
Add Attachment
</button>
@include('company.templates.attachment.create')
<!-- End Attachment Modal -->
<!-- Begin Attachment Modal Script -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script type="text/javascript">
jQuery(function($)
$('#uploadModal').on('shown.bs.modal')
);
$(document).ready(function()
$(".btn-submit").click(function()
var lsthmtl = $(".clone").html();
);
$("body").on("click",".btn-success-add",function()
var lsthmtl = $(".clone").html();
$(".increment").after(lsthmtl);
);
$("body").on("click",".btn-danger",function()
$(this).parents(".hdtuto").remove();
);
);
</script>
<!-- End Attachment Modal Script -->
子视图 - 资源/视图/公司/模板/附件/create.blade.php
<form action="attachment/create.blade.php" enctype="multipart/form-data" method="post">
<div class="modal fade" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="uploadModalTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="uploadModalLongTitle">Upload Attachment</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container lst">
csrf_field()
<div class="input-group hdtuto control-group lst increment" >
<input type="file" name="filenames[]" class="myfrm form-control">
<div class="input-group-btn">
<button class="btn btn-success btn-success-add" type="button"><i class="fldemo glyphicon glyphicon-plus"></i>Add</button>
</div>
</div>
<div class="clone hide">
<div class="hdtuto control-group lst input-group" style="margin-top:10px">
<input type="file" name="filenames[]" class="myfrm form-control">
<div class="input-group-btn">
<button class="btn btn-danger" type="button"><i class="fldemo glyphicon glyphicon-remove"></i> Remove</button>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-submit btn-primary">Submit</button>
</div>
</div>
</div>
</div>
</form>
我可以说存储操作没有被使用,因为在“成功”上传后,我的存储文件夹或数据库表中没有任何内容。似乎与路线有关。
【问题讨论】:
【参考方案1】:你有一个表单动作:
action="attachment/create.blade.php"
根据您的路由文件,您的实际端点是:
/company/company_id/templates/template_id/attachment
.
您的表单操作需要指向上述 URI,并使用正确的路由参数 company_id
和 template_id
。
您的代码正在尝试发布到 attachment/create.blade.php
,这是不正确的...
长话短说,您需要将attachment/create.blade.php
替换为与您的路线相匹配的适当表单操作端点。
我看不到其余代码,因此很难就如何生成操作 URI 提供更多建议,但您需要的是:
<form action=" action('AttachmentController@store', ['company_id' => $companyId, 'template_id' => $templateId]) " enctype="multipart/form-data" method="post">
当然这是假设$companyId
和$templateId
都可用。
【讨论】:
感谢您的协助。我最初在表单操作中使用了类似的东西,但最终硬编码文件路径以试图强制命中端点。我尝试了您的建议,但我的控制器没有正确传递 ID,但考虑到这一点,我尝试了类似:以上是关于子视图刀片中的 Laravel 表单无法发布上传的主要内容,如果未能解决你的问题,请参考以下文章