laravel基本程序不使用路由

时间:2014-06-24 03:47:20

标签: laravel

我正在尝试使用LARAVEL框架开发Web应用程序,并成功将Laravel安装在我的笔记本电脑中。

我想制作一个基本的控制器和一个视图程序。和路由。我的程序中是否有任何错误,请回答这个问题。

我的控制器,视图,路由文件如下所述

NewController.php

<?php

class New_Controller extends BaseController {



public function action_index()()
{
    return View::make('hai');
}

}


hai.php

Laravel Basics    

<body>
<h1>Jishad is Developing Laravel 4</h1>
</body>
</html>

Routes.php

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

Route::get('/', function()
{
return View::make('hai');
});

2 个答案:

答案 0 :(得分:0)

1.您的路由错误,如果您想将路由指向控制器,请执行以下操作:

Route::get('/', 'NewController@action_index');

2.如果您的控制器名称为NewController,那么您的班级也应该是:

class NewController extends BaseController {

  public function action_index()
  {
    return View::make('hai');
  }

}

3.此外,public function action_index()()应为public function action_index()

答案 1 :(得分:0)

您可以尝试以下内容。声明路线(当您访问index页时,它会从NewController调用home方法):

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

现在像这样创建NewController

// NewController.php
class NewController extends BaseController {

    // You may keep this line in your BaseController
    // so you don't need to use it in every controller
    protected $layout = 'layouts.master';

    public function index()
    {
        // Make the view and pass a $name variable to the view with
        // Jishad as it's value and then set the $view to the layout
        $view = View::make('hai')->with('name', 'Jishad');
        $this->layout->content = $view;
    }
}

现在,由于您不熟悉此框架,因此建议您使用控制器布局而不是blade布局,但您可能会发现layout/templating here的所有内容。要使其正常工作,您需要在master文件夹中创建app/views/layouts布局,如下所示:

// app/views/layouts/master.php
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Simple Web Page</title>
    </head>
    <body>
        <div><?php echo $content; ?></div>
    </body>
</html>

还需要在hai文件夹中创建app/views视图,例如:

// hai.php
<h1>Welcome TO Laravel</h1>
<p><?php echo $name ?> is developing learning Laravel</p>

您需要详细了解Laravel,查看Laravel - 4文档并阅读一些articles/books。您还使用了action_index,但它已在Laravel - 3中使用,只需使用index