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 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.

    点赞 评论 复制链接分享

相关推荐