第三章初识laravel,helloWorld的MVC实现

来源:互联网 发布:知福茶叶公司 编辑:程序博客网 时间:2024/04/28 05:53

第三章初识laravel,helloWorld的MVC实现

 

一、简单粗暴的laravel4部曲

Laravel的程序主干,对我们使用者来说,主要就是4步:

第一步:绑定路由

第二:写controller

第三步:写model

第四步:写view

本章教程将严格按照这四步为你演示基于mvc的“helloworld”,让你了解laravel程序的一般开发流程。

二,基于laravel的hello world实现:

1、绑定路由

打开phpstorm,打开“app\Http\routes.php”,文件,添加以下代码:

Route::get('hello','helloController@index');

 

2、写model

右键“app\Http”,新建一个文件夹“model”,然后在该文件夹下新建一个“hello.php”文件。


在hello.php中添加以下代码:

<?php namespaceApp\Http\model;

useIlluminate\Database\Eloquent\Model;

class hello extends Model {

    public function index() {

        return ['msg' => 'hello World!'];

    }

}


3、写controller

右键“app\Http\controller”文件夹,“新建”,“文件”,输入文件名“helloController.php”,点“确定”

 

然后在文件中“helloController.php”中输入以下代码:

<?php namespaceApp\Http\Controllers;

use App\Http\model\hello;

class helloController extendsController {

    protected $model;

    public function __construct(hello $hello) {

        $this->model = $hello;

    }

    public function index() {

        returnview('hello',$this->model->index());

    }

}


4、写view


在“resources\views”文件夹下,新建一个“hello.blade.php”文件,输入以下代码:

{{$msg}}

 

5、打开浏览器,输入网址

http://127.0.0.1:1022/hello

好了,不出意外,你应该能看到如下界面:

 

恭喜你,基于laravelhello world写好了。完整的mvc实现。

三、结语:

本章所示的就是laravel的mvc完整开发流程,我们的后续开发都离不开这4步,当然,如果你不走MVC,会更简单。阅读至此,你会发现,你未对laravel进行任何配置,是的,laravel就是这么神奇,当天下第一框架thinkPHP还在rewrite rule时,你的helloword已经写好了。清晰简单的url,高度自由的文档结构(laravel的MVC目录都是可以更改的,这将在以后的介绍中说明)。

0 0
原创粉丝点击