search
HomeWeb Front-endHTML Tutorial实现简单blog展示_html/css_WEB-ITnose

创建一个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 id="hello">hello</h1>    <hr />    @foreach($articles as $article)  //循环输出        <h2 id="article-title">{{$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 id="hello">hello</h1>    <hr />    @foreach($articles as $article)        {{--<h2 id="a-href-articles-article-id-article-title-a"><a href="/articles/{{$article->id}}">{{$article->title}}</a></h2>--}}{{--        <h2 id="a-href-url-articles-article-id-article-title-a"><a href="{{url('articles',$article->id)}}">{{$article->title}}</a></h2>--}}        <h2 id="a-href-action-ArticlesController-show-article-id-article-title-a"><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的结合)

Statement
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
What is the root tag in an HTML document?What is the root tag in an HTML document?Apr 29, 2025 am 12:10 AM

TheroottaginanHTMLdocumentis.Itservesasthetop-levelelementthatencapsulatesallothercontent,ensuringproperdocumentstructureandbrowserparsing.

Are the HTML tags and elements the same thing?Are the HTML tags and elements the same thing?Apr 28, 2025 pm 05:44 PM

The article explains that HTML tags are syntax markers used to define elements, while elements are complete units including tags and content. They work together to structure webpages.Character count: 159

What is the significance of <head> and <body> tag in HTML?What is the significance of <head> and <body> tag in HTML?Apr 28, 2025 pm 05:43 PM

The article discusses the roles of <head> and <body> tags in HTML, their impact on user experience, and SEO implications. Proper structuring enhances website functionality and search engine optimization.

What is the difference between <strong>, <b> tags and <em>, <i> tags?What is the difference between <strong>, <b> tags and <em>, <i> tags?Apr 28, 2025 pm 05:42 PM

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

Please explain how to indicate the character set being used by a document in HTML?Please explain how to indicate the character set being used by a document in HTML?Apr 28, 2025 pm 05:41 PM

Article discusses specifying character encoding in HTML, focusing on UTF-8. Main issue: ensuring correct display of text, preventing garbled characters, and enhancing SEO and accessibility.

What are the various formatting tags in HTML?What are the various formatting tags in HTML?Apr 28, 2025 pm 05:39 PM

The article discusses various HTML formatting tags used for structuring and styling web content, emphasizing their effects on text appearance and the importance of semantic tags for accessibility and SEO.

What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?What is the difference between the 'id' attribute and the 'class' attribute of HTML elements?Apr 28, 2025 pm 05:39 PM

The article discusses the differences between HTML's 'id' and 'class' attributes, focusing on their uniqueness, purpose, CSS syntax, and specificity. It explains how their use impacts webpage styling and functionality, and provides best practices for

What is the 'class' attribute in HTML?What is the 'class' attribute in HTML?Apr 28, 2025 pm 05:37 PM

The article explains the HTML 'class' attribute's role in grouping elements for styling and JavaScript manipulation, contrasting it with the unique 'id' attribute.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.