Rauluco CM
Rauluco CM

Reputation: 1

Why is 'method not found' appearing when using Resourceful controller?

I'm trying to create a restful api with Laravel 4 but all the time I get an error:

"Symfony\Component\HttpKernel\Exception\NotFoundHttpException","message":"Controller method not found."

I follow the explanation on routes-first-in-first-out. But I had no luck.

My routes looks like:

Route::group(array('prefix' => 'api/v1'), function(){
    Route::resource('contact', 'ContactController');
});

Route::get('/', function(){
    return View::make('hello');
});

And the method in the controller looks like

public function store()
{
 // ... somecode       
}

And I have no idea what I'm doing wrong. I already search here but I keep having the same result.

Does anyone know where is the problem?

Upvotes: 0

Views: 1483

Answers (2)

reikyoushin
reikyoushin

Reputation: 2021

You need to learn more about Restful Controllers first. If you look closely on the docs page, on the table there..

your action store says:

Verb    Path        Action  Route Name
POST    /resource   store   resource.store

which means:

  1. store can only be triggered when using a POST request (the verb on the table above).
  2. the path is /resource, in your case api/v1/contact
  3. if you are going to redirect to it using a named route, you will use the route name (ex: Redirect::route('api/v1/contact.create') goes to add page) Note: redirecting to store would make no sense so i used create instead

Back to your question..

Going via the browser with a URL of 'api/v1/contact/store' will produce a GET request to that route, but store expects a POST request. if you submit a POST from a form to the store URL, it will succeed but going to it via browser(GET) will surely produce an error saying the route cannot be found since you don't have a get 'api/v1/contact/store' route declared..

Upvotes: 1

Rauluco CM
Rauluco CM

Reputation: 1

I found that after the post request that i made originally with javascript, that a redirect is triggerso taht it fails. I don't know why. The .htacces file is the original from laravel intalation and the virtual host is :

<VirtualHost *:80>
        ServerName raulcm.laravel
        DocumentRoot /var/www/raulcm.laravel/public
        <Directory /var/www/raulcm.laravel/public>
                Options Indexes FollowSymLinks Includes ExecCGI
                DirectoryIndex index.php
                AllowOverride All
                Order deny,allow
                Allow from all
        </Directory>
        ErrorLog /var/www/raulcm.laravel/logs/error.log
        CustomLog /var/www/raulcm.laravel/logs/access.log common
</VirtualHost>

Upvotes: 0

Related Questions