【发布时间】: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>
我可以说存储操作没有被使用,因为在“成功”上传后,我的存储文件夹或数据库表中没有任何内容。似乎与路线有关。
【问题讨论】:
标签: php laravel post view routes