在Laravel 5.2上路由文件路径时405(方法不允许)

时间:2016-07-18 16:06:24

标签: php html5 laravel laravel-blade

在用户帐户更新请求中,UserController.php为:

    public function postSaveAccount(Request $request) {

            $this->validate($request, [
                'first_name' => 'required|max:120',
            ]);

            $user = Auth::user();
            $user->first_name = $request['first_name'];
            $user->update();
            $file = $request->file('image');
            //todo: treat image and upload other formats, maybe unique hash.
            $filename = $request['first_name'] . '-' . $user->id . '.jpg';
            if ($file) {
                Storage::disk('local')->put($filename, File::get($file));
            }
            return redirect()->route('account');
        }

    public function getUserImage($filename) {
        $file = Storage::disk('local')->get($filename);
        return new Response($file, 200);
     }

在routes.php上:

       Route::get('/account', [
          'uses' => 'UserController@getAccount',
          'as' => 'account',
          'middleware' => 'auth',
        ]);

        Route::post('/updateaccount', [
            'uses' => 'UserController@postSaveAccount',
            'as' => 'account.save',
            'middleware' => 'auth',
        ]);

        Route::post('/userimage/{filename}', [
            'uses' => 'UserController@getUserImage',
            'as' => 'account.image',
        ]);

并且在观点上:

@section('content')
    <section class="row new-post">
        <div class="col-md-6 col-md-offset-3">
            <header><h3>Your Account</h3></header>
            <form action="{{ route('account.save') }}" method="post" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="first_name">First Name</label>
                    <input type="text" name="first_name" class="form-control" value="{{ $user->first_name }}" id="first_name">
                </div>
                <div class="form-group">
                    <label for="image">Image (only .jpg)</label>
                    <input type="file" name="image" class="form-control" id="image">
                </div>
                <button type="submit" class="btn btn-primary">Save Account</button>
                <input type="hidden" value="{{ Session::token() }}" name="_token">
            </form>
        </div>
    </section>
    @if (Storage::disk('local')->has($user->first_name . '-' . $user->id . '.jpg'))
        <section class="row new-post">
            <div class="col-md-6 col-md-offset-3">
                <img src="{{ route('account.image', ['filename' => $user->first_name . '-' . $user->id . '.jpg']) }}" alt="" class="img-responsive">
            </div>
        </section>
    @endif
@endsection

我得到了405(方法不允许)。 文件正确上传,我使用正确的外观。 它只是没有正确显示的网址路线,有什么建议吗?

TNKS。

3 个答案:

答案 0 :(得分:1)

您的account.image路由应该使用Route :: get而不是Route :: post

答案 1 :(得分:0)

route('account')似乎不存在?你需要创建它。

答案 2 :(得分:0)

好的,发现了Bug:

在routes.php上应该是:

#include "Mtest.h"
#define LOG_TAG "Test"
int mtest(const char *str){
    ALOGE("[Lucien] test(%s)", str);
    return 0;
}