douyu9433 2017-02-19 21:26
浏览 31
已采纳

Laravel相同的控制器使用一条路线而不是另一条路线

Update

To avoid all the debug and questions about the ajax call I created a index method in the controller with only a echo 'hey!', and still getting the same stuff:

app.url/blocktest give me a hey, but changing to app.url/pictures, commenting out one route and uncommenting the other give-me a 404 error.

Update and

I'm building my first application with laravel (version 5.4) but in the process of make a image upload with crop my route (originally photos) start getting a 404 error, after hours of debugging I get nothing, so changing the route and it started working, then blocked again, so I get a little confused and I think I'm really don't getting something here. my actual routes:

  Route::resource('partners', 'PartnersController');
  //Route::resource('pictures', 'PicturesController');
  Route::resource('blocktest', 'PicturesController');

this way everything works fine, my ajax post to my PicturesController and everything goes well, here is the controller function:

public function store(Request $request)
    {
        // echo 'hey';
        $requests = request()->file('picture');
        foreach ($requests as $picture) {
            $filename  = time() . '.' . $picture->getClientOriginalExtension();
            $path = public_path('pictures/' . $filename);
            Image::make($picture->getRealPath())->crop(600, 600)->save($path);


            $picture = new Picture;
            $picture->partner_id = $request['partner_id'];
            $picture->src = $filename;
            $picture->save();
        }
    }

but if I change the routes to:

Route::resource('partners', 'PartnersController');
Route::resource('pictures', 'PicturesController');
// Route::resource('blocktest', 'PicturesController')

trying to use the pictures and point the ajax to the new route I get a 404 from nginx.

Here's the result of a php artisan route:list

+--------+-----------+-------------------------+------------------+-----------------------------------------------------------+--------------+
| Domain | Method    | URI                     | Name             | Action                                                    | Middleware   |
+--------+-----------+-------------------------+------------------+-----------------------------------------------------------+--------------+
|        | GET|HEAD  | api/user                |                  | Closure                                                   | api,auth:api |
|        | GET|HEAD  | partners                | partners.index   | FrutoProibido\Http\Controllers\PartnersController@index   | web          |
|        | POST      | partners                | partners.store   | FrutoProibido\Http\Controllers\PartnersController@store   | web          |
|        | GET|HEAD  | partners/create         | partners.create  | FrutoProibido\Http\Controllers\PartnersController@create  | web          |
|        | GET|HEAD  | partners/{partner}      | partners.show    | FrutoProibido\Http\Controllers\PartnersController@show    | web          |
|        | PUT|PATCH | partners/{partner}      | partners.update  | FrutoProibido\Http\Controllers\PartnersController@update  | web          |
|        | DELETE    | partners/{partner}      | partners.destroy | FrutoProibido\Http\Controllers\PartnersController@destroy | web          |
|        | GET|HEAD  | partners/{partner}/edit | partners.edit    | FrutoProibido\Http\Controllers\PartnersController@edit    | web          |
|        | GET|HEAD  | pictures                | pictures.index   | FrutoProibido\Http\Controllers\PicturesController@index   | web          |
|        | POST      | pictures                | pictures.store   | FrutoProibido\Http\Controllers\PicturesController@store   | web          |
|        | GET|HEAD  | pictures/create         | pictures.create  | FrutoProibido\Http\Controllers\PicturesController@create  | web          |
|        | GET|HEAD  | pictures/{picture}      | pictures.show    | FrutoProibido\Http\Controllers\PicturesController@show    | web          |
|        | PUT|PATCH | pictures/{picture}      | pictures.update  | FrutoProibido\Http\Controllers\PicturesController@update  | web          |
|        | DELETE    | pictures/{picture}      | pictures.destroy | FrutoProibido\Http\Controllers\PicturesController@destroy | web          |
|        | GET|HEAD  | pictures/{picture}/edit | pictures.edit    | FrutoProibido\Http\Controllers\PicturesController@edit    | web          |
+--------+-----------+-------------------------+------------------+-----------------------------------------------------------+--------------+

I think it's a pretty strange behavior, but maybe I'm just not seeing something.

thanks!

  • 写回答

1条回答 默认 最新

  • doudaochu1699 2017-02-19 21:31
    关注

    The problem here is probably like this:

    1) You defined:

    Route::resource('pictures', 'PicturesController');
    

    so you would like to get run for example

    http://example.com/pictures
    

    to get list of pictures

    2) But you also created pictures directory in public directory so when you run

     http://example.com/pictures
    

    server will try to display content of pictures directory in public directory

    It won't work. You need to use this url either for controller or for files in public directory. Otherwise you will get this strange behaviour.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改