Currently I'm working with laravel. When I want to acces a controller through the view. I receive the error that my method is not found.
Action App\Http\Controllers\RidesController@edit not defined. (View: C:\wamp\wwwesources\viewsidesides.blade.php)
But when I try the index method it works strange enough? What should I do?
This is my rides.blade.php:
</thead>
<tbody>
<tbody>
@foreach($rides as $ride)
<tr>
<td><a href="{!! action('RidesController@edit',$ride->id) !!}">{!! $ride->id !!}</a> </td>
<td>{!! $ride->created_at !!}</td>
<td>{!! $ride->beginstand !!}</td>
<td>{!! $ride->eindstand !!}</td>
<td>{!! $ride->van !!}</td>
<td>{!! $ride->naar !!}</td>
<td>{!! $ride->bezoekadres !!}</td>
<td>{!! $ride->geredenroute !!}</td>
<td>{!! $ride->karakterrit !!}</td>
<td>{!! $ride->toelichting !!}</td>
<td>{!! $ride->kilometerszakelijk !!}</td>
<td>{!! $ride->kilometersprive !!}</td>
</tr>
@endforeach
</tbody>
And my routes controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\modelsidesregistration;
use App\Http\Requests\RideFormRequest;
class RidesController extends Controller
{
public function index()
{
$rides = ridesregistration::all();
return view("rides.rides",compact('rides'));
}
public function create()
{
return view('rides.create');
}
public function store(rideFormRequest $request)
{
$rides = new ridesregistration(array(
'beginstand' =>$request->get('beginstand'),
'eindstand' =>$request->get('eindstand'),
'van' =>$request->get('van'),
'naar' =>$request->get('naar'),
'bezoekadres' =>$request->get('bezoekadres'),
'geredenroute' =>$request->get('geredenroute'),
'karakterrit' =>$request->get('karakterrit'),
'toelichting' =>$request->get('toelichting'),
'kilometerszakelijk' =>$request->get('kilometerszakelijk'),
'kilometersprive' =>$request->get('kilometersprive')
));
$rides->save();
return redirect('/maakrit')->with('message','Rit '.$rides->id.' is succesvol aangemaakt.');
}
public function edit($id)
{
$rides = ridesregistration::whereId($id)->firstOrFail();
return view('rides.edit',compact('rides','rides'));
}
}
According to laravel 5 documentaion http://laravel.com/docs/5.0/controllers
u should use namespacing with action() helper
if we will dig deeper into \Illuminate\Routing\UrlGenerator::action
we`ll see:
public function action($action, $parameters = [], $absolute = true)
{
if ($this->rootNamespace && !(strpos($action, '\\') === 0)) {
$action = $this->rootNamespace.'\\'.$action;
} else {
$action = trim($action, '\\');
}
if (!is_null($route = $this->routes->getByAction($action))) {
return $this->toRoute($route, $parameters, $absolute);
}
throw new InvalidArgumentException("Action {$action} not defined.");
}
that your action('RidesController@index') works because it is defined in routes. and your action('RidesController@edit',$ride->id) not work becouse it is not in the routes.
define this controller action in your app/Http/routes.php