zoukankan      html  css  js  c++  java
  • laravel怎么创建一个简单的blog

    主要功能实现:点击标题跳转

    第一步:创建路由:

    Route::get('/articles','ArticlesController@index');
    Route::get('/articles/{id}','ArticlesController@show');
    表示访问../article时会执行index方法
    访问.../article/{id}时会执行show方法
    第二步:创建控制器

    写index和show方法
    public function index(){
    $articles = Article::all();
    return view('articles.index',compact('articles'));
    }
    表示跳articles目录下的index界面,传递参数articles
    public function show($id){
    $article = Article::findOrFail($id);//articles是mysql中的表
    表的结构为:


    //dd($article);
    /*if(is_null($article)){
    abort(404);
    }*/
    return view('articles.show',compact('article'));
    }

    第三步:写index和show界面
    index.blade.php
    @extends('app')
    @section('content')
    <h1>article</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>
    <article>
    <div class="body">
    {{ $article->contents }}
    </div>
    </article>
    @endforeach
    @stop

    show.blade.php
    @extends('app')
    @section('content')
    <h1>{{ $article->title }}</h1>
    <hr>
    <article>
    <div class="body">
    {{ $article->contents }}
    </div>
    </article>
    @stop


  • 相关阅读:
    第十篇 .NET高级技术之委托
    第九篇 .NET高级技术ref、out
    文华财经函数大全
    为字段创建全文检索索引
    C#.NET中代码注释提示
    WPF中的资源引用心得
    XAML文件动态加载
    spring MVC找不到JS的问题
    Oracle性能监控脚本
    ExtJs之Ext.data.Store
  • 原文地址:https://www.cnblogs.com/caimuqing/p/5410730.html
Copyright © 2011-2022 走看看