laravel NotFoundHttpException

江枫思渺然 提交于 2019-12-04 00:23:25

问题


I am new at laravel. I am trying to make a link to another page. I have the page index and want to go to desc which display information about a vehicle selected in index page. The problem is that it shows the error:

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException

index.blade.php

    @foreach ($cars as $car)
           <tr>                                   
           <td> 
           {{link_to_action('CarController@show',  $car->Description, $car->id)}}</td>  
             {{ Form::open(array('action' => 'CarController@show', $car->id)) }}
                  {{ Form::close() }}
                        <td>{{ $car->License }}</td>  
                        <td>{{ $car->Milage }}</td> 
                        <td>{{ $car->Make }}</td>  
                        <td>{{ $car->status }}</td>                                    
          </tr>                                                            
    @endforeach

routes.php

Route::resource('/', 'CarController');
Route::resource('create', 'DataController');
Route::post('desc', array('uses' => 'CarController@show'));
Route::post('create', array('uses' => 'CarController@create', 'uses' => 'DataController@index'));
Route::post('update', array('uses' => 'CarController@update'));
Route::post('store', array('store' => 'CarController@store'));

回答1:


The 'NotFoundHttpException' means Laravel wasn't able to find a route to for the request.

Your desc route is a POST route, only, and a link_to_action will create a GET request, so you may need to change add a GET route too:

Route::post('desc', array('uses' => 'CarController@show'));
Route::get('desc', array('uses' => 'CarController@show'));

There's also a any, which does GET, POST, PUT, DELETE:

Route::any('desc', array('uses' => 'CarController@show'));

If you need to get and id from your route, you will have to add it as a parameter:

Route::post('car/{id}', array('uses' => 'CarController@show'));

And you will have to access your page as:

http://myappt.al/public/car/22

But if you want to access it as:

http://myappt.al/public/22

You'll need to do:

Route::post('{id}', array('uses' => 'CarController@show'));

But this one is dangerous, because it will may grab all the routes, so you MUST to set it as your very last route.

And your controller must accept that parameter:

class CarController extends Controller {

   public function show($id)
   {
      dd("I received an ID of $id");
   }
}

EDIT:

Since you most of your routes made manually, you can also go with the index this way:

Route::resource('create', 'DataController'); 

Route::get('/', 'CarController@index');

Route::post('create', array('uses' => 'CarController@create','uses' => 'DataController@index')); 
Route::post('update', array('uses' => 'CarController@update')); 
Route::post('store', array('store' => 'CarController@store')); 

Route::get('{id}', array('uses' => 'CarController@show'));
Route::post('{id}', array('uses' => 'CarController@show'));


来源:https://stackoverflow.com/questions/23430205/laravel-notfoundhttpexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!