简体   繁体   中英

Laravel 4 controllers and routes

Heloo, im making a laraver 4 app for first time and some things i still dont understand, one of them are the controllers:

At laraver 3 i could do something like:

Route::get('/',array('uses'=>'home@index'));

and at the controller:

public function get_index(){
    return 'hello';
}

But now at laravel 4 i have something like:

class HomeController extends BaseController {

    public function showWelcome()
    {
        return View::make('home.main');
    }

}

First in this file could i change HomeController to Home only? The how could i make the controller restful?

And at the routes:

Route::get('/', array('uses'=>'HomeController@Welcome'));

How can i call it, it's not working.

Didn't understand at all laravel 4 documentation.

The method showWelcome is not RESTful and as such it has a route that maps to it directly. If you want your methods on HomeController to be prefixed with the HTTP verb they respond to, such as get , post , put , then you need to register the controller with Route::controller .

Route::controller('/', 'HomeController');

You should then rename the method to getWelcome and you would browse to it by hitting localhost/yourapp/welcome . You'd use getIndex if you wanted to hit localhost/yourapp .

It's already RESTful, you just had a small bug on your route, try this:

Route::get('/', array('uses'=>'HomeController@showWelcome'));

In the uses you must have ControllerName@methodName , so it's showWelcome in your case, not just Welcome .

Also, you can change it to Home, but you better stick with the name HomeController (or AnythingController), unless you have a very good reason for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM