您好,欢迎来到欧得旅游网。
搜索
您的当前位置:首页实现简单blog展示_html/css

实现简单blog展示_html/css

来源:欧得旅游网

创建一个index主页列出所有文章

生成路由

app/Http/routes.php

Route::get('/articles','ArticlesController@index');

生成一个controller

php artisan make:controller ArticlesController

namespace App\Http\Controllers;use App\Articles; //当你使用Articles这个model的时候,phpstorme会帮你默认导入这个类use Illuminate\Http\Request;use App\Http\Requests;class ArticlesController extends Controller{ // public function index(){ //因为路由里面我们使用了ArticlesController@index,所以这里是index方法 $articles = Articles::all(); //这里需要注意的是这个Articles是我们创建的那个Articles的model,在phpstorme会有提示,这里的all方法是返回这个model查询到的所有数据,这些数据是提前输入好的 return $articles; }}

访问localhost:8000/articles会出现以下内容,laravel会默认返回json格式的数据

本页JSON数据由FeHelper进行自动格式化,若有任何问题,点击这里提交 意见反馈[{"id": 1,"title": "title","content": "content","publish_at": "2016-05-14 18:04:44","created_at": "2016-05-14 18:04:48","updated_at": "2016-05-14 18:04:48"},{"id": 2,"title": "title2","content": "content2","publish_at": "2016-05-15 04:24:48","created_at": "2016-05-14 18:07:42","updated_at": "2016-05-14 18:07:42"},{"id": 3,"title": "second title","content": "second content","publish_at": "2016-05-14 18:15:38","created_at": "2016-05-14 18:15:38","updated_at": "2016-05-14 18:15:38"}]

运用已有的blade知识,配置一个layout

例如

resources/views/layout/app.blade.phplayout只是一个概念,这个文件主要是为了创造一个模板容器,用来处理那些可以复用的代码,例如一些html的head,body标签

< !DOCTYPE html> Laravel  @yield('content') @yield(('footer'))

创建刚才控制器指定的那个blade文件

resources/views/articles/index.blade.php

@extends('layout.app')@section('content') 

hello


@foreach($articles as $article) //循环
输出

{{$article->title}}

{{$article->content}}

Copyright © 2019- ovod.cn 版权所有 湘ICP备2023023988号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务