为了避免所有关于ajax调用的调试和问题,我在控制器中创建了一个只有echo 'hey!'
的索引方法,并且仍然得到相同的东西:
app.url / blocktest给我一个hey
,但更改为app.url / pictures,注释掉一条路线并取消注释另一条路线给我一个404
错误。
我正在使用laravel(版本5.4)构建我的第一个应用程序但是在使用crop我的路径(原始照片)进行图像上传的过程中开始出现404错误,经过数小时的调试后我什么都没得到,所以改变路线,它开始工作,然后再次阻止,所以我有点困惑,我想我真的没有在这里得到一些东西。我的实际路线:
Route::resource('partners', 'PartnersController');
//Route::resource('pictures', 'PicturesController');
Route::resource('blocktest', 'PicturesController');
这样一切正常,我的ajax发布到我的PicturesController,一切顺利,这是控制器功能:
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();
}
}
但如果我将路线更改为:
Route::resource('partners', 'PartnersController');
Route::resource('pictures', 'PicturesController');
// Route::resource('blocktest', 'PicturesController')
尝试使用pictures
并将ajax指向新路线,我从nginx获得404.
这是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 |
+--------+-----------+-------------------------+------------------+-----------------------------------------------------------+--------------+
我认为这是一种非常奇怪的行为,但也许我只是没有看到什么。
谢谢!
答案 0 :(得分:1)
这里的问题可能是这样的:
1)你定义了:
Route::resource('pictures', 'PicturesController');
所以你希望以示例的方式运行
http://example.com/pictures
获取图片列表
2)但你也在pictures
目录中创建了public
目录,所以当你运行时
http://example.com/pictures
服务器将尝试在pictures
目录
public
目录的内容
它无效。您需要将此URL用于控制器或public
目录中的文件。否则你会得到这种奇怪的行为。