实现简单blog展示_html/css_WEB-ITnose

WBOY
풀어 주다: 2016-06-21 08:48:34
원래의
1338명이 탐색했습니다.

创建一个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><html><head>    <title>Laravel</title>    <!-- 新 Bootstrap 核心 CSS 文件 -->    <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css"/></head><body>    <div class="container">        @yield('content')    </div>    @yield(('footer'))</body></html>
로그인 후 복사

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

resources/views/articles/index.blade.php

@extends('layout.app')@section('content')    <h1>hello</h1>    <hr />    @foreach($articles as $article)  //循环输出        <h2>{{$article->title}}</h2>        <article>            <div>                {{$article->content}}            </div>        </article>    @endforeach@stop
로그인 후 복사

创建一个文章页,列出单个文章

单个文章,需要有文章id在url里面区分,所以特意建立了一个新的路由和一个新的controller方法show

Route::get('/articles/{id}','ArticlesController@show'); {id}传入变量参数,用大括号来标识,传入变量到controller的show方法

app/Http/Controllers/ArticlesController.php

class ArticlesController extends Controller{    //    public function index(){        $articles = Articles::all();//        return $articles;        return view('articles.index',compact('articles'));    }    public function show($id){    //获取从路由传入的$id变量        $article = Articles::find($id); //使用laravel提供的find方法查询数据库//        return $articles;        return view('articles.show',compact('article'));    }}
로그인 후 복사

resources/views/articles/show.blade.php

@extends('layout.app')@section('content')    <h1>hello</h1>    <hr />    @foreach($articles as $article)        {{--<h2><a href="/articles/{{$article->id}}">{{$article->title}}</a></h2>--}}{{--        <h2><a href="{{url('articles',$article->id)}}">{{$article->title}}</a></h2>--}}        <h2><a href="{{action('ArticlesController@show',[$article->id])}}">{{$article->title}}</a></h2>        //这里有三种方法来传递变url量到blade中,第一种是直接写的,第二种是通过url方法,使用的路由方法,第三种是通过action方法,使用的是controller方式        <article>            <div>                {{$article->content}}            </div>        </article>    @endforeach@stop
로그인 후 복사

本文由 PeterYuan 创作,采用 署名-非商业性使用 2.5 中国大陆 进行许可。 转载、引用前需联系作者,并署名作者且注明文章出处。神一样的少年 » 实现简单blog展示(主要使用到Eloquent和Controller的结合)

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!