search
Homephp教程php手册Laravel 5框架学习之模型、控制器、视图基础流程,laravel框架

Laravel 5框架学习之模型、控制器、视图基础流程,laravel框架

添加路由

复制代码 代码如下:
Route::get('artiles', 'ArticlesController@index');

创建控制器

复制代码 代码如下:
 php artisan make:controller ArticlesController --plain

修改控制器

<&#63;php namespace App\Http\Controllers;

use App\Article;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class ArticlesController extends Controller {

 public function index() {
    $articles = Article::all();

    return $articles;
  }

}

可以在浏览器中看到返回的 JSON 结果,cool!

修改控制器,返回视图

 public function index() {
    $articles = Article::all();

    return view('articles.index', compact('articles'));
  }

创建视图

@extends('layout')

@section('content')
  <h1 id="Articles">Articles</h1>

  @foreach($articles as $article)
    <article>
      <h2 id="article-title">{{$article->title}}</h2>

      <div class="body">{{$article->body}}</div>
    </article>
  @endforeach
@stop

浏览结果,COOL!!!!

显示单个文章

添加显示详细信息的路由

复制代码 代码如下:
Route::get('articles/{id}', 'ArticlesController@show');

其中,{id} 是参数,表示要显示的文章的 id,修改控制器:

  public function show($id) {
    $article = Article::find($id);

    //若果找不到文章
    if (is_null($article))
    {
      //生产环境 APP_DEBUG=false
      abort(404);
    }
    return view('articles.show', compact('article'));
  }

laravel 提供了更加方便的功能,修改控制器:

  public function show($id) {
    $article = Article::findOrFail($id);

    return view('articles.show', compact('article'));
  }

It's cool.

新建视图

@extends('layout')

@section('content')
  <h1 id="article-title">{{$article->title}}</h1>

  <article>
    {{$article->body}}
  </article>
@stop

在浏览器中尝试访问:/articles/1 /articles/2

修改index视图

@extends('layout')

@section('content')
  <h1 id="Articles">Articles</h1>
  <hr/>
  @foreach($articles as $article)
    <article>
      <h2>
        {{--这种方式可以--}}
        <a href="/articles/{{$article->id}}">{{$article->title}}</a>
        {{--这种方式更加灵活,不限制路径--}}<br>
        <a href="{{action('ArticlesController@show', [$article->id])}}">{{$article->title}}</a>
        {{--还可以使用--}}<br>
        <a href="{{url('/articles', $article->id)}}">{{$article->title}}</a>
      </h2>

      <div class="body">{{$article->body}}</div>
    </article>
  @endforeach
@stop

以上所述就是本文的全部内容了,希望能够对大家学习Laravel5框架有所帮助。

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

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)