laravel-上传文件 [英] laravel - uploading files

查看:64
本文介绍了laravel-上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天大家好,我正在尝试构建一个电子商务网站Laravel应用程序.我只是在学习使用此框架进行的文件上传,最终在尝试上传内容时导致此错误:

Hello everyone today I am trying to build a Laravel application that is an e-commerce website. I am just learning about file uploads using this framework that end up resulting in this error when I try uploading something:

> MethodNotAllowedHttpException in RouteCollection.php line 219:

    in RouteCollection.php line 219
    at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 206
    at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'PUT', 'PATCH', 'DELETE')) in RouteCollection.php line 158
    at RouteCollection->match(object(Request)) in Router.php line 823
    at Router->findRoute(object(Request)) in Router.php line 691
    at Router->dispatchToRoute(object(Request)) in Router.php line 675
    at Router->dispatch(object(Request)) in Kernel.php line 246
    at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
    at CheckForMaintenanceMode->handle(object(Request), object(Closure))
    at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
    at Pipeline->Illuminate\Routing\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
    at Pipeline->then(object(Closure)) in Kernel.php line 132
    at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99
    at Kernel->handle(object(Request)) in index.php line 54

我看了该网站上的laracast视频,发现文件上传中有过时的视频.我对Laravel框架有些陌生,希望可以在上传和验证文件方面获得一些帮助

I've looked at the laracast videos on that website and found an outdated video on file uploads. I am somewhat new to the Laravel framework and I am hoping I get some help with uploading and validating files

这是我的route.php文件的样子

Here's what my routes.php file looks like

        /*
    |--------------------------------------------------------------------------
    | Application Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register all of the routes for an application.
    | It's a breeze. Simply tell Laravel the URIs it should respond to
    | and give it the controller to call when that URI is requested.
    |
    */
    Route::resource('item', 'ItemController');

    Route::get('welcome', function() {
        return view('welcome');
    });

    Route::post('item', 'ItemController@Store');
    Route::auth();

    Route::get('/home', 'HomeController@index');

EDIT my item controller

    <?php

namespace App\Http\Controllers;

use Request;

use App\Http\Requests;

use App\Item;
class ItemController extends Controller
{
    public function index()
    {
        //fetch all items from the database
        $items = Item::All();
        return $items;
    }

    public function show($id)
    {

        $item = Item::find($id);

        if (is_null($item)) {
            abort(404);
        }
        return view('item.show', compact('item'));
    }

    /**
     * @return mixed
     */
    public function create()
    {
        return view('item.create');
    }

    public function store(Requests\CreateItem $request)
        {


            Item::create($request->all());

            if($request->hasFile('filename')) {
                $file = $request->file('filename');
                if($request->file('photo')->isValid()) {
                    $request->file('filename')->move('/uploads');
                }

            }

        }

我的表单代码

    @extends('app');
@section('content');
    <h1>Add a  new item</h1>
    <hr />
    <content>
        <div class="form-group">
        {!! Form::open() !!}
        {!! Form::label('name', "Name") !!}
        {!! Form::text('name', null, ['class' => 'form-control']) !!}

       {!! Form::label('filename', "File Name") !!}
        {!! Form::file('filename', null, ['class' => 'form-control']) !!}

        {!! Form::label('description', 'Description') !!}
        {!! Form::textarea('description', null, ['class' => 'form-control']) !!}
        {!! Form::submit('Add Item', ['class' => 'btn btn-primary form-control']) !!}

    </content>
</div>

@stop

推荐答案

//Route
Route::post('item', ['as' => 'item.store', 'uses' => 'ItemController@store']);

//Form

替换

{!! Form::open() !!}

{!! Form::open() !!}

{!! Form::open(['route' => 'item.store', 'files' => true]) !!}`

{!! Form::open(['route' => 'item.store', 'files' => true]) !!}`

这篇关于laravel-上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆