简体   繁体   English

Laravel 4控制器和路线

[英]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: Heloo,我是第一次制作laraver 4应用,我仍然不了解某些事情,其中​​之一是控制器:

At laraver 3 i could do something like: 在laraver 3上,我可以执行以下操作:

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: 但是现在在laravel 4我有类似的东西:

class HomeController extends BaseController {

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

}

First in this file could i change HomeController to Home only? 首先,我可以将HomeController更改为Home吗? 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. 完全不了解laravel 4文档。

The method showWelcome is not RESTful and as such it has a route that maps to it directly. showWelcome方法不是RESTful的,因此它具有直接映射到它的路由。 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 . 如果您希望HomeController上的方法以它们响应的HTTP动词作为前缀,例如getpostput ,那么您需要向Route::controller注册Route::controller

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

You should then rename the method to getWelcome and you would browse to it by hitting localhost/yourapp/welcome . 然后,您应该将方法重命名为getWelcome ,然后getWelcome localhost/yourapp/welcome来浏览至该方法。 You'd use getIndex if you wanted to hit localhost/yourapp . 如果您想访问localhost/yourapp则可以使用getIndex

It's already RESTful, you just had a small bug on your route, try this: 它已经是RESTful了,您的路线上只有一个小错误,请尝试以下操作:

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

In the uses you must have ControllerName@methodName , so it's showWelcome in your case, not just Welcome . 在使用中,您必须具有ControllerName@methodName ,因此在您的情况下为showWelcome ,而不仅仅是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. 另外,您可以将其更改为Home,但最好使用名称HomeController(或AnythingController),除非您有充分的理由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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