laravel - 上传文件

时间:2016-04-24 18:44:10

标签: php laravel

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

> 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框架有点新,我希望我能帮助上传和验证文件

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

        /*
    |--------------------------------------------------------------------------
    | 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

1 个答案:

答案 0 :(得分:2)

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

//Form

替换

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

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