获取/发布web.php创建/存储路线返回404

时间:2019-10-28 12:02:48

标签: php laravel

我试图弄清为什么我的创建路由为什么会返回404错误,因为它仅给我一个加载资源失败:服务器在服务器上回复了404(未找到)状态页。

显示,编辑和更新路线功能没有问题,并且视图与create.blade.php文件位于同一文件夹中。我的web.php似乎可以识别控制器中的create函数,并且该控制器看起来至少可以从vs代码的Follow Link系统中识别出create.blade.php文件。我还尝试清空刀片文件,仅保留div,extends和section,但仍然收到404。

web.php

    this->ui->cb_RealWorld->setWindowFlags(this->ui->cb_RealWorld->windowFlags() | Qt::WindowTransparentForInput);

Location.php

Route::get('/location/{location}', 'LocationController@show')->name('location.show');
Route::get('/location/create', 'LocationController@create');
Route::post('/location', 'LocationController@store');
Route::get('/location/{location}/edit', 'LocationController@edit')->name('location.edit');
Route::patch('/location/{location}', 'LocationController@update')->name('location.update');

LocationController.php

<?php

namespace App;
use Illuminate\Database\Eloquent\Model;

class Location extends Model
{

    public function casino() {
        return $this->belongsTo(Casino::class);
    }

    public function duties() {
        return $this->hasMany(Duty::class)->orderBy('title', 'ASC');
    }

}

create.blade.php

<?php

namespace App\Http\Controllers;
use App\Location;
use Illuminate\Http\Request;

class LocationController extends Controller
{
    public function create() {
        return view('locations.create');
    }

    public function store() {
        $data = request()->validate([
            'title' => 'required',
        ]);
        return redirect("/casino/" . location()->casino()->id);
    }

    public function edit(Location $location) {
        return view('locations.edit', compact('location'));
    }

    public function update( Request $request, Location $location) {

        $data = request()->validate([
            'title' => 'required'
        ]);

        $location->title = $request->get('title');
        $location->save();

        return redirect("/location/{$location->id}");
    }

    public function show(Location $location) {
        return view('locations.show', compact('location'));
    }

}

2 个答案:

答案 0 :(得分:3)

您的路线顺序错误。

由于显示路线/location/{location}位于顶部,它将捕获/location/create并假定create是您要查看的位置的路线参数,因此您永远不会到达create操作,但转到show操作。

create路由置于show路由上方应该可以解决此问题:

Route::get('/location/create', 'LocationController@create');
Route::post('/location', 'LocationController@store');
Route::get('/location/{location}', 'LocationController@show')->name('location.show');
Route::get('/location/{location}/edit', 'LocationController@edit')->name('location.edit');
Route::patch('/location/{location}', 'LocationController@update')->name('location.update');

答案 1 :(得分:0)

您没有为创建路径创建别名。应该是这个吗?

Route::get('/location/create', 'LocationController@create')->name('location.create');
Route::post('/location', 'LocationController@store')->name('location.store');