在 laravel 5.3 中未找到路由异常也尝试了资源路由和手动路由
Route Not found exception in laravel 5.3 tried resource route and manual route as well
我创建了一个资源 BrandController
然后创建了它的路由。问题是有些路线有效,有些则无效。例如,create
路由不起作用。我也试过手动声明路由,但问题是一样的。我 运行 命令像
php artisan route:clear
php artisan cache:clear
这是路线
Route::group(['namespace' => 'AppControllers'], function () {
/*
|--------------------------------------------------------------------------
| All routes of BrandController are defined here
|--------------------------------------------------------------------------
|
*/
Route::get('brands', 'BrandController@index')->name('brand.index');
Route::get('brand/create', 'BrandController@create')->name('brand.create');
Route::get('brand/edit/{id}', 'BrandController@edit')->name('brand.edit');
Route::delete('brand/delete/{id}', 'BrandController@destroy')->name('brand.destroy');
Route::post('brand/store', 'BrandController@store')->name('brand.store');
Route::post('brand/update/{id}', 'BrandController@update')->name('brand.update');
// Here is resource route
Route::resource('brands', 'BrandController');
});
我在这里创建了一个简单的 a
标签:
<a href="{{route('brand.create')}}">Add New</a>
每当我单击此 link 时,它会将 /
转换为 dot
,就像这样
它还生成了
但同样的问题仍然存在。 NotFoundHttpException
在路由集合中
控制器代码:
<?php
namespace App\Http\Controllers\AppControllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Brand;
class BrandController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//dd('jgh');
//$brands = brand::all();
return view('brands.index');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return redirect('brands.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(BrandRequest $request)
{
$input = $request->all();
$storeBrand = new Brand();
$storeBrand->create($input);
//return redirect->()->back();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$editBrand = Brand::findOrFail($id);
return view('brands.edit',compact('editBrand'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(BrandRequest $request, $id)
{
$updateBrand = Brand::findOrFail($id);
$input = $request->all();
$updateBrand->update($input);
return redirect()->back();
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$deleteBrand = Brand::findOrFail($id);
$deleteBrand->delete();
return redirect()->back();
}
}
像这样制作你的资源路由
Route::resource('brand', 'BrandController', [
'names' => [
'index'=>'brand.list',
'create'=>'brand.create',
'store'=>'brand.store',
'update'=>'brand.update',
'edit'=>'brand.edit',
'show'=>'brand.show',
'destroy'=>'brand.remove',
]
]);
更改您的 create
方法,如下所示
public function create()
{
return redirect('brands/create');
}
.
符号在 redirect
方法中不起作用...
您可以使用:
public function create()
{
return redirect()->route('brands.create');
}
要么
public function create()
{
return redirect('brands/create');
}
但我建议您使用第一个,因为我们在其中使用了路由名称。如果您想更改 url 它们,则不必担心此代码。
我创建了一个资源 BrandController
然后创建了它的路由。问题是有些路线有效,有些则无效。例如,create
路由不起作用。我也试过手动声明路由,但问题是一样的。我 运行 命令像
php artisan route:clear
php artisan cache:clear
这是路线
Route::group(['namespace' => 'AppControllers'], function () {
/*
|--------------------------------------------------------------------------
| All routes of BrandController are defined here
|--------------------------------------------------------------------------
|
*/
Route::get('brands', 'BrandController@index')->name('brand.index');
Route::get('brand/create', 'BrandController@create')->name('brand.create');
Route::get('brand/edit/{id}', 'BrandController@edit')->name('brand.edit');
Route::delete('brand/delete/{id}', 'BrandController@destroy')->name('brand.destroy');
Route::post('brand/store', 'BrandController@store')->name('brand.store');
Route::post('brand/update/{id}', 'BrandController@update')->name('brand.update');
// Here is resource route
Route::resource('brands', 'BrandController');
});
我在这里创建了一个简单的 a
标签:
<a href="{{route('brand.create')}}">Add New</a>
每当我单击此 link 时,它会将 /
转换为 dot
,就像这样
它还生成了
但同样的问题仍然存在。 NotFoundHttpException
在路由集合中
控制器代码:
<?php
namespace App\Http\Controllers\AppControllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Brand;
class BrandController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//dd('jgh');
//$brands = brand::all();
return view('brands.index');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return redirect('brands.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(BrandRequest $request)
{
$input = $request->all();
$storeBrand = new Brand();
$storeBrand->create($input);
//return redirect->()->back();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$editBrand = Brand::findOrFail($id);
return view('brands.edit',compact('editBrand'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(BrandRequest $request, $id)
{
$updateBrand = Brand::findOrFail($id);
$input = $request->all();
$updateBrand->update($input);
return redirect()->back();
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$deleteBrand = Brand::findOrFail($id);
$deleteBrand->delete();
return redirect()->back();
}
}
像这样制作你的资源路由
Route::resource('brand', 'BrandController', [
'names' => [
'index'=>'brand.list',
'create'=>'brand.create',
'store'=>'brand.store',
'update'=>'brand.update',
'edit'=>'brand.edit',
'show'=>'brand.show',
'destroy'=>'brand.remove',
]
]);
更改您的 create
方法,如下所示
public function create()
{
return redirect('brands/create');
}
.
符号在 redirect
方法中不起作用...
您可以使用:
public function create()
{
return redirect()->route('brands.create');
}
要么
public function create()
{
return redirect('brands/create');
}
但我建议您使用第一个,因为我们在其中使用了路由名称。如果您想更改 url 它们,则不必担心此代码。