Laravel ::路线与控制器 [英] Laravel :: Routes Vs. Controller

查看:71
本文介绍了Laravel ::路线与控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我在Codeigniter呆了几个月之后对laravel 4还是陌生的,所以我浏览了很多有关laravel的教程,我想清楚的一件事是laravel中Routes和Controller之间的实际区别是什么,因为我们也可以在控制器和路由中创建和生成视图.谁能在laravel中简要说明何时使用路由和Controller?因为在其他Framework中,我们需要路由来指定应用程序中的某些特定URL,并且Controller用于执行一些实际任务,但是在laravel中,除了路由机制之外,我没有获得Routes的主要概念吗?

As I am new to the laravel 4 after spending some few months in Codeigniter, I went through the lots of tutorials about laravel and one thing I want to be clear is what is the actual difference between Routes and Controller in laravel, because we can create and generate view in both controller and routes too. Will anyone explain me in brief when to use routes and Controller in laravel? Because in other Framework we need routes to specify some particular URL's within the apps and Controller were used to do some real tasks but in laravel I didnt get the main concept of Routes except the routing mechanism?

推荐答案

可以在Routes::any()中获取视图或做很多事情的事实与MVC和逻辑分离背道而驰.

The fact that you can get views or do a lot of things in Routes::any() is against MVC and separation of logic.

Route::get("admin", function(){})中,您确实可以快速访问路由回调,否则必须以标准方式将其绑定到控制器.但是Laravel允许您在闭包(function(){})中完成工作,而不是将其绑定到控制器.无论如何,它可以让您,但最好避免使用它.在Route::get()中,您仅应使用路由",仅此而已.

In Route::get("admin", function(){}), you indeed have a fast access to your route callback, which otherwise in a standard fashion must just be bound to controller. But Laravel allows you to do your job there in a closure (function(){}), instead of binding it to a controller. Anyway, it lets you, but you'd better avoid it. In Route::get() you only should go with your 'routing' and nothing more.

除非有测试或一些琐碎的请求,否则您没有理由在Route中使用回调.因此,最好避免这种情况:

There is no reason for you to use callbacks in Route unless for testing or some trivial requests. So, better to avoid this:

Route::get("admin", function(){
    return View::make("admin_index");
});

宁愿这样:

Route::controller("admin", "AdminController");

在您的AdminController.php中:

// I mean create a file named AdminController.php in controllers directory under app.
class AdminController extends Controller
{
   function getIndex()
   {
      return View::make("admin_index");
   }
}

了解有关Route::controller和静态控制器的更多信息.

Read more about Route::controller and restful controllers.

一些注意事项:

  • 具有在路线中添加闭合的功能,使您能够 复杂的路线决策并具有强大的路线系统.

  • Having the ability to add closures in your Routes allows you to make complex decisions on Routes and have a powerful routing system.

这些回调使您可以为路由添加条件.

These callbacks let you add conditions to your route.

将Controller与您分隔开来使您成为应用程序
更可扩展,更少混乱,并使其他编码人员更多
将来很舒服.

Having Controller separated from you Routes makes you application
more extensible, less confusing and makes other coders more
comfortable in future.

它使您可以更好地关注自己的问题并找到解决方案,
这种物理上的分离非常重要.具有View :: make()
在您的路线内部将所有问题相互融合,并构成一个
编码人员感到困惑.

It allows you to focus better on your problem and finding solution,
this physical separation is very important. Having View::make()
inside your Route stirs all problems into each other and makes up a
confusion for the coder.

这篇关于Laravel ::路线与控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆