Laravel 5 framework learning model, controller, view basic process, laravel framework_PHP tutorial

WBOY
Release: 2016-07-13 09:57:46
Original
919 people have browsed it

Laravel 5 framework learning model, controller, view basic process, laravel framework

Add routing

Copy code The code is as follows:
Route::get('artiles', 'ArticlesController@index');

Create Controller

Copy code The code is as follows:
php artisan make:controller ArticlesController --plain

Modify controller

<&#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;
  }

}

Copy after login

You can see the returned JSON results in the browser, cool!

Modify the controller and return to the view

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

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

Copy after login

Create View

@extends('layout')

@section('content')
  <h1>Articles</h1>

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

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

Copy after login

Browse the results, COOL! ! ! !

Show single article

Add route showing details

Copy code The code is as follows:
Route::get('articles/{id}', 'ArticlesController@show');

Among them, {id} is a parameter, indicating the id of the article to be displayed. Modify the controller:

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

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

Copy after login

Laravel provides more convenient functions to modify the controller:

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

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

Copy after login

It's cool.

New View

@extends('layout')

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

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

Copy after login

Try to access: /articles/1 /articles/2

Modify index view

@extends('layout')

@section('content')
  <h1>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
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone learning the Laravel5 framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/980212.htmlTechArticleLaravel 5 framework learning model, controller, view basic process, laravel framework adds routing copy code The code is as follows: Route ::get('artiles', 'ArticlesController@index'); Create control...
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