Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel action not defined

After updating from Laravel 4.2 to 5.0, I am getting the following message in almost every page of my application:

InvalidArgumentException in UrlGenerator.php line 561: Action ArticlesController@create not defined.

In my routes.php file I have:

Route::get('articles/create', ['as' => 'articles.create', 'uses' => 'ArticlesController@create']);
Route::post('articles/create', ['as' => 'articles.create.handle', 'uses' => 'ArticlesController@handleCreate']);

And in my controller:

class ArticlesController extends Controller {

    public function create()
    {
        $input=null;
        if (Input::old()) {
            $input = Input::old();
        }
        $tagsJson = Tag::all()->toJson();
        $categories = ArticleCategory::all();
        return View::make('admin.articles.create', compact(array('tagsJson', 'categories', 'input')));
    }

    public function handleCreate()
    {
        $input = Input::all();

        if ($input['op']=="preview") {
            return redirect()->action('ArticlesController@create')->withInput();
        } else if ($input['op']=="post") {
            //
        }

    }
}

The error I get comes from this line:

return redirect()->action('ArticlesController@create')->withInput();

Any help? Thanks, Ilias

like image 741
Ilias Kouroudis Avatar asked Apr 23 '15 11:04

Ilias Kouroudis


2 Answers

You are getting this error because Laravel 5 uses namespacing by default. The official Laravel 5 upgrade guide says the following about migrating your controllers:

Since we are not going to migrate to full namespacing in this guide, add the app/Http/Controllers directory to the classmap directive of your composer.json file. Next, you can remove the namespace from the abstract app/Http/Controllers/Controller.php base class. Verify that your migrated controllers are extending this base class.

In your app/Providers/RouteServiceProvider.php file, set the namespace property to null.

Listed here under "controllers".

The last line is probably the one that will solve your issue.

like image 65
Maarten00 Avatar answered Sep 20 '22 15:09

Maarten00


I was getting the same error message, and it was because I'd completely forgotten that I needed to add this to routes/web.php:

Route::get('myUrlPath', 'HomeController@myActionName');

I had been trying to do this from another controller action:

return redirect()->action('HomeController@myActionName')->with('windowTitle', 'Error. Please contact us.')->with('message', $message);

like image 37
Ryan Avatar answered Sep 16 '22 15:09

Ryan