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

WBOY
Release: 2016-06-21 08:48:34
Original
1337 people have browsed it

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

生成路由

app/Http/routes.php

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

生成一个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;    }}
Copy after login

访问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"}]
Copy after login

运用已有的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>
Copy after login

创建刚才控制器指定的那个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
Copy after login

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

单个文章,需要有文章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'));    }}
Copy after login

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
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!