【问题标题】:Laravel connection between controller, views and routes控制器、视图和路由之间的 Laravel 连接
【发布时间】:2014-08-26 18:15:36
【问题描述】:

我是 laravel 的新手,我正在尝试弄清楚如何链接视图和 url。我有这个 HomeController.php:

class HomeController extends BaseController {

    protected $layout = 'layouts.master';

    public function index(){

        $this->layout->title = 'Web Title';
        $this->layout->content = View::make('home');

    }

}

它有一个包含以下内容的 layouts.master (views/layouts/master.blade.php):

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
    <link type="text/css" rel="stylesheet" href="{{URL::asset('css/styles.css')}}" />
</head>
<body>
    <div class="wrapper">
        <div class="header">

        </div>
        <div class="sidebar">
            asdasd
        </div>
        <div class="mainContent">
            @yield('content')
        </div><!--mainContent End-->
    </div>
</body>
</html>

这是根“views”文件夹中的 home.blade.php

@section('content')
    <div class="userSearchInfo">
        <div class="userPhoto"></div>
        <div class="userData">
            <div class="userName">
                Name
            </div>
        </div>
    </div>
@stop

最后是routes.php,我想问题出在这里,我不明白它是否可以,它因网站而异,我真的不知道我应该使用什么,laravel api不擅长全部,没有这方面的信息

Route::get('/', 'HomeController@index');

希望你能帮忙,谢谢。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    在您的routes.php 文件中,您声明应用程序响应的URLs,例如,您在routes.php 文件中有以下路由声明:

    Route::get('/', 'HomeController@index');
    

    它告诉框架,每当请求登陆页面时,应该调用HomeController控制器的index方法。

    因此,路由用于在您的应用程序中注册URL,并且根据URL 的请求,将执行使用该路由注册的操作。在您的示例中,/ 是用于登录/主页的URL,您为该URL 注册的操作是'HomeController@index',这意味着index 方法将从HomeController 执行。

    通过在app/config/app.php 文件中设置debug =&gt; true 来启用调试模式,这样您就可以跟踪错误,因为在启用debug 模式后,您将获得信息丰富的错误详细信息。此外,Laravel 是学习框架基础知识的最佳场所,因此请正确阅读文档。

    【讨论】:

    • 奇怪,在我的 localhost/laravel/public 上它说:哎呀,好像出了点问题。
    • wow thx for debug info :D 问题是:$this->layout->title = 'Web Title';错误是:使用未定义的常量标题 - 假定为“标题”
    • 欢迎并很高兴知道这一点,它帮助了你:-)
    猜你喜欢
    • 2012-12-17
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2017-03-05
    • 2020-06-07
    • 1970-01-01
    • 2012-11-11
    相关资源
    最近更新 更多