weixin_33701251 2016-08-02 07:40 采纳率: 0%
浏览 24

Laravel 5.2路由

I have a strange problem with Laravel 5.2. I have been updating someone else project for a while now, and most things work as they should by now, however, I have a problem with a route. I use PHP’s built-in error_log(); to see what code is actually executed.

On my page, I have a button for uploading images, and once an image is selected, it is supposed to be uploaded via an Ajax call:

resources/views/assignments/create.blade.php


…
<script>
…
var $file = $("#file");
        /*$file.before('<button class="clickFile btn btn-success" data-id="#file" type="button">
        {{Lang::get($lang_short_name.'.selectImage')}}</button>');
         $('.clickFile').click(function(){
         $($(this).attr('data-id')).click();
         });*/
        $file.change(function (e) {
            e.preventDefault();
            var myFormData = new FormData();
            myFormData.append('file', $(this)[0].files[0]);
            console.log('views/assignments/create.blade.php line 717: trying to upload in background');
            $.ajax({
                url        : "{{url('assignment/uploadImage')}}",
                type       : "POST",
                data       : myFormData,
                contentType: false,
                cache      : false,
                processData: false,
                success    : function (data) {
                    var obj = jQuery.parseJSON(data);
                    initCropper(obj.url);
                }
            });
            $(this).val('');
        });
…
</script>

Now, I don’t think the problem is in this AJAX call, which is why I left out the before and after, but correct me if I am wrong, and I’ll put up the rest.

The browser console gives an error 500:

views/assignments/create.blade.php line 717: trying to upload in background
jquery-1.11.3.min.js:5 POST http://localhost:8080/assignment/uploadImage 500 (Internal Server Error)
send @ jquery-1.11.3.min.js:5
ajax @ jquery-1.11.3.min.js:5
(anonymous function) @ edit:1201
dispatch @ jquery-1.11.3.min.js:4r.handle @ jquery-1.11.3.min.js:4
  • please note that the first line is what I wrote as console.log in the ajax call.

In the router, I made the following test, which was successful:

app/Http/routes.php

…
Route::group(['middleware' => 'auth'], function () {
…
Route::post('assignment/uploadImage', error_log('test'));
…
}

However, when I try to actually point to my controller, nothing happens. Here is the code I use to redirect to my controller:

app/Http/routes.php

…
Route::group(['middleware' => 'auth'], function () {
…
Route::post('assignment/uploadImage', ['uses' => 'AssignmentController@postImage']);
…
}

Now, this call is inside a routing group, using authentication middleware, but since the test code above worked, I assume this not to be a problem, again, correct me if I am wrong, and I will post the entire router.

In my controller, I have the following code

app/Http/Controllers/AssignmentController.php

<?php namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Assignment;
use App\AssignmentFork;
use App\AssignmentView;
use App\Curriculum;
use App\Grade;
use App\Http\Requests;
use App\Langs;
use App\RoboticSolution;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;


class AssignmentController extends Controller
{

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{

    error_log('assignmentController index called');

    $assignments = Assignment::with(['user.profile'])->orderBy('id', 'desc')->paginate(20);

    return view('assignments.index')
        ->with('assignments', $assignments)
        ->with('mySaves', Auth::user()->savedAssignments);
}

public function create(){…}
public function store(){…}
public function show($id){…}
public function edit($id){…}
public function update($id, $data){…}
public function destroy($id){…}

public function postImage(){

error_log('attempted image upload......................');
    if (Input::hasFile('file')) {
        if (substr(Input::file('file')->getMimeType(), 0, 5) == 'image') {
            // this is an image
            $foto_name = str_random(10) . '_' . time() . '.' . Input::file('file')->getClientOriginalExtension();
            Input::file('file')->move(public_path() . "/pictures/", 'full_' . $foto_name);

            return json_encode(['name' => $foto_name, 'url' => asset("/pictures/" . 'full_' . $foto_name)]);
        }
    }

}


public function myDrafts(){…}
public function myPublished(){…}
public function getInfo(){…}

When I google this problem, the solution I find is that mod_rewrite is not turned on, but I checked that already, and even tried running the code on a different system, where I also checked that mod_rewrite was turned on, so I don’t think that is the actual problem.

I also get some results about a wrongly formatted route, as it seems there has been some changes for this in the update to Laravel 5.2; but I can't seem to find the error myself, and not for lack of trying.

Please let me know if I should expand any of the code here, I just tried to keep it a bit brief for readability. Any help would be greatly appreciated.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效
    • ¥15 悬赏!微信开发者工具报错,求帮改
    • ¥20 wireshark抓不到vlan
    • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
    • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持