重定向错误laravel

时间:2016-11-20 12:37:57

标签: laravel redirect

您好我正在为我的创建表单使用createProduct页面。在我创建了一个产品后,我想用参数重定向到同一页面。你能帮我解决以下错误吗?

web.php

Route::get('/admin/products/create','productsController@createProduct');
Route::post('/admin/products/creating','productsController@creatingProduct');

创建功能

public function creatingProduct(){

    $product = new Product();
    $product->name = Input::get('name');
    $product->description = Input::get('description');
    $product->price = Input::get('price');
    $categories = Category::all();

    try {
       $product->save();
       $pageMessage =  prepareMessage("alert-success","Yahoooo!!","Eklendiii");
    } catch ( \Illuminate\Database\QueryException $e) {
       $pageMessage =  prepareMessage("alert-danger","Üzgünüz!!","Ürününüz eklenemedi");
    }

     //  return view('admin.createProduct',compact('categories','pageMessage'));


     return Redirect::route('/admin/products/create')->with( 'pageMessage', $pageMessage );
}

创建功能

public function createProduct(){

    $categories = Category::all();
    return view('admin.createProduct',compact('categories'));

}

createProduct.bladde.php

    @if(isset($pageMessage))
    {!!$pageMessage!!}
    @endif  
    <form class="well form-horizontal" action="  {{url('admin/products/creating')}}" method="POST"  id="contact_form">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <!--{{ Form::open(['url' => '/admin/products/create', 'files' => true]) }}-->
<div class="form-group">
  <label class="col-md-4 control-label">Ürün İsmi</label>  
  <div class="col-md-8 inputGroupContainer">

ERROR

InvalidArgumentException in UrlGenerator.php line 314:
Route [/admin/products/create] not defined.

3 个答案:

答案 0 :(得分:2)

您正在将网址传递给Redirect :: route(),而该网址需要路由的名称。

return redirect('/admin/products/create')->with( 'pageMessage', $pageMessage );

如果您使用旧版本的Laravel,我相信它会是

return Redirect::to('/admin/products/create')->with( 'pageMessage', $pageMessage );

您可以设置命名路线并使用它,它非常简单:

Route::post( '/admin/products/creating', [
    'uses' => 'productsController@creatingProduct',
    'as'  => 'products.create'
]);

好处是您可以在整个应用程序中引用路径名称,如果您决定更改URL的格式,则只需在一个位置执行此操作。

答案 1 :(得分:0)

如果您使用route(),则应使用路线名而不是网址:

return redirect()->route('products.create');

所以你可以命名你的路线:

Route::get('/admin/products/create', ['as' => 'products.create', 'uses' => 'productsController@createProduct']);

作为替代方案,您可以使用以下网址:

return redirect('/admin/products/create');

答案 2 :(得分:0)

您定义的Redirect::route()未在路线文件中定义。

应该如下:

return Redirect::to('/admin/products/create')->with( 'pageMessage', $pageMessage );