2015年8月5日 星期三

ubuntu伺服器設定

ubuntu 14.04.2
套件
apt-get install apache2 php5 php5-mcrypt mysql-server-5.5 git

appache2 enable rewrite

a2enmod rewrite

apache設定檔
AllowOverride All

網站根目錄laravel/public

/var/www/html
/var/www/laravel/public
ln -s /var/www/laravel/public var/www/html/myapp

http://123.456.789.1/myapp

.env設定要改


2015年8月4日 星期二

熱門文章

Route加路徑
Route::get('posts/hot', ['as'=>'posts.hot','uses'=>'PostController@hot']);

PostController加hot function
public function hot(){    $posts = Post::with('user')        ->where('page_views', '>=' , 100)        ->orderBy('page_views', 'desc')        ->paginate(5) ;    //dd($posts);    return view('posts.index', compact('posts'));
}

nav.blade.php的超連結設定路由

 nav.blade.php的超連結設定路由

<a class="navbar-brand" href="{{ route('home.index') }}">我的部落格</a>
</div>
<div class="navbar-collapse collapse navbar-inverse-collapse">
    <ul class="nav navbar-nav">
        <li class="active"><a href="{{ route('posts.index') }}"><i class="fa fa-list-ul my-icon"></i> 文章總覽</a></li>
        <li><a href="{{ route('posts.hot') }}"><i class="fa fa-fire my-icon"></i> 熱門文章</a></li>
        <li><a href="about.html"><i class="fa fa-info-circle my-icon"></i> 關於本站</a></li>
    </ul>

route加入hot路由
Route::get('posts/hot', ['as'=>'posts.hot','uses'=>'PostController@hot']);

2015年8月3日 星期一

給route路徑別名


字串變陣列
Route::get('/', PostController@index');
Route::get('posts', 'PostController@index');
Route::get('/posts/{id}', 'PostController@show');

陣列
Route::get('/', ['as'=>'home.index','uses'=>'PostController@index']);
Route::get('posts', ['as'=>'post.index','uses'=>'PostController@index']);
Route::get('posts/{id}', ['as'=>'post.show','uses'=>'PostController@show']);

index.blade.php
加入超連結
{{ route('posts.show', [$post->id]) }}
文章只有30篇若localhost:8000/posts/31會有錯誤訊息
在PostController
public function show($id){   $post =Post::find($id);    //如果文章不存在回傳    if(!$post){
        return '文章不存在';
              }    return view('posts.show', compact('post'));}

文章標題製作超連結可以連結到每篇文章的內容

index.blade.php

<div class="panel panel-default my-article">
    <div class="panel-heading">
        <h4><a href="{{ url('posts/'.$post->id) }}">{{ $post->id }}{{ $post->title }}</a></h4>
    </div>
    <div class="panel-body">
        {{ $post->content }}
    </div>
    <div class="panel-footer">
        作者: <a href="oneuserposts.html">{{ $post->user->name }}</a>
        <div class="pull-right">    發表於: {{ $post->created_at }}</div>
        <div class="pull-right">觀看次數:  {{ $post->page_views }} 次</div>
        <div class="my-tags">
            Tags:<a href="tag-posts.html?tag=1"><span class="tag">標籤1</span></a>
            <a href="tag-posts.html?tag=2"><span class="tag">標籤tag2</span></a>
            <a href="tag-posts.html?tag=3"><span class="tag">tag3</span></a>
        </div>
    </div>
</div>

Route::pattem('id', '[0-9]+

Route的放置順序很重要

id限定格視為數字



//id限制格式
Route::pattern('id', '[0-9]+');
Route::get('/', 'PostController@index');Route::get('posts', 'PostController@index');Route::get('/posts/{id}', 'PostController@show'); Route::get('/test', function(){ $user = \App\Post::with('user')->find(1); dd($user);});