Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions CONTRIBUTING.md

This file was deleted.

113 changes: 57 additions & 56 deletions app/controllers/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,61 @@

class BlogController extends BaseController {

// set this controller's layout
protected $layout = 'layouts.master';

public function index()
{
$this->layout->content = View::make('blog.index', array(
'posts' => Post::all()
));
}

// display a blog post creation form
public function newPost()
{
$this->layout->content = View::make('blog.new');
}

// create a post that has been POSTed to the server
public function createPost()
{
// get the post
$post = new Post();
$post->title = Input::get('title');
$post->content = nl2br(Input::get('content'));
$post->save();

return Redirect::route('viewPost', array('id' => $post->id));
}

// view a post by id
public function viewPost($id)
{
// get the post
$post = Post::findOrFail($id);

$this->layout->content = View::make('blog.view', array(
'post' => $post
));
}

// create a comment for a given post
public function createComment($id)
{
// get the post that the user commented on
$post = Post::findOrFail($id);

// create a new comment
$comment = new Comment();
$comment->name = Input::get('name');
$comment->content = nl2br(Input::get('content'));

// save the comment with a relation to the post
$post->comments()->save($comment);

// go back to the post
return Redirect::route('viewPost', array('id' => $post->id));
}
// set this controller's layout
protected $layout = 'layouts.master';

// homepage controller
public function index()
{
$this->layout->content = View::make('blog.index', array(
'posts' => Post::all()
));
}

// display a blog post creation form
public function newPost()
{
$this->layout->content = View::make('blog.new');
}

// create a post that has been POSTed to the server
public function createPost()
{
// get the post
$post = new Post();
$post->title = Input::get('title');
$post->content = nl2br(Input::get('content'));
$post->save();

return Redirect::route('viewPost', array('id' => $post->id));
}

// view a post by id
public function viewPost($id)
{
// get the post
$post = Post::findOrFail($id);

$this->layout->content = View::make('blog.view', array(
'post' => $post
));
}

// create a comment for a given post
public function createComment($id)
{
// get the post that the user commented on
$post = Post::findOrFail($id);

// create a new comment
$comment = new Comment();
$comment->name = Input::get('name');
$comment->content = nl2br(Input::get('content'));

// save the comment with a relation to the post
$post->comments()->save($comment);

// go back to the post
return Redirect::route('viewPost', array('id' => $post->id));
}
}
10 changes: 5 additions & 5 deletions app/database/migrations/2014_11_07_155735_create_posts_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public function up()
{
Schema::create('posts', function($table)
{
$table->increments('id');
$table->string('title');
$table->text('content');
$table->timestamps();
$table->increments('id');
$table->string('title');
$table->text('content');
$table->timestamps();
});
}

Expand All @@ -28,7 +28,7 @@ public function up()
*/
public function down()
{
Schema::drop('users');
Schema::drop('posts');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public function up()
{
Schema::create('comments', function($table)
{
$table->increments('id');
$table->string('name');
$table->text('content');
$table->integer('post_id')->unsigned();
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade');
$table->timestamps();
$table->increments('id');
$table->string('name');
$table->text('content');
$table->integer('post_id')->unsigned();
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade');
$table->timestamps();
});
}

Expand Down
3 changes: 2 additions & 1 deletion app/models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

class Comment extends Eloquent {

// many-to-one relationship with the Post model
public function post()
{
return $this->belongsTo('Post');
return $this->belongsTo('Post');
}

}
16 changes: 15 additions & 1 deletion app/models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@

class Post extends Eloquent {

// one-to-many relationship with the Comment model
public function comments()
{
return $this->hasMany('Comment');
return $this->hasMany('Comment');
}

// helper function to get the URL of a post
public function getURL()
{
return URL::action('viewPost', array('id' => $this->id));
}

// helper function to get a string of the number of comments
public function getNumCommentsStr()
{
$num = $this->comments()->count();

if ($num == 1)
{
return '1 comment';
}

return $num . ' comments';
}
}
16 changes: 8 additions & 8 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@

// routes for the blog
Route::get('/post/new', array(
'as' => 'newPost',
'uses' => 'BlogController@newPost'
'as' => 'newPost',
'uses' => 'BlogController@newPost'
));

Route::post('/post/new', array(
'as' => 'createPost',
'uses' => 'BlogController@createPost'
'as' => 'createPost',
'uses' => 'BlogController@createPost'
));

Route::get('/post/{id}', array(
'as' => 'viewPost',
'uses' => 'BlogController@viewPost'
'as' => 'viewPost',
'uses' => 'BlogController@viewPost'
));

Route::post('/post/{id}/comment', array(
'as' => 'createComment',
'uses' => 'BlogController@createComment'
'as' => 'createComment',
'uses' => 'BlogController@createComment'
));
5 changes: 3 additions & 2 deletions app/views/blog/partials/post.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
{{ str_limit($post->content, 430) }}
@endif
</div>
<footer>
<p class="text-muted">Posted {{ $post->created_at->diffForHumans() }}</p>
<footer class="text-muted">
<p>Posted {{ $post->created_at->diffForHumans() }}</p>
<p><a href="{{ $post->getURL(); }}#comments">{{ $post->getNumCommentsStr() }}</a></p>
</footer>
</article>
24 changes: 15 additions & 9 deletions app/views/blog/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@

<hr />

<section>
<h3 class="page-title">Comments</h3>
@foreach ($post->comments as $comment)
<div class="comment">
<p><strong>{{ $comment->name }} says...</strong></p>
<blockquote>{{ $comment->content }}</blockquote>
</div>
@endforeach
<section id="comments">
<h3 class="title">Comments</h3>
@if (count($post->comments) === 0)
<p>No comments yet on this post.</p>
@else
@foreach ($post->comments as $comment)
<div class="comment">
<p><strong>{{ $comment->name }} says...</strong></p>
<blockquote>{{ $comment->content }}</blockquote>
</div>
@endforeach
@endif
</section>

<p class="lead">Add a comment</p>
<section>
<h3 class="title">Add a comment</h3>
<form action="{{ URL::route('createComment', array('id' => $post->id)) }}" method="post">
<div class="form-group">
<input name="name" class="form-control" type="text" placeholder="Your name" />
Expand Down
18 changes: 16 additions & 2 deletions public/css/style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* General style tweaks */
body {
padding: 50px 0;
padding-top: 50px;
}

@media (min-width: 768px) {
Expand All @@ -20,6 +21,15 @@ h1 a:hover {
text-decoration: none;
}

section {
margin-bottom: 50px;
}

section .title {
margin-bottom: 20px;
}

/* Post specific styling */
article, .comment {
margin-bottom: 50px;
}
Expand All @@ -28,8 +38,12 @@ article header {
padding-bottom: 10px;
}

article header h1 {
font-size: 26px;
}

article footer {
padding: 10px 0;
padding-top: 20px;
}

article .content {
Expand Down
42 changes: 27 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
## Laravel PHP Framework
## Blog50

[![Build Status](https://travis-ci.org/laravel/framework.svg)](https://travis-ci.org/laravel/framework)
[![Total Downloads](https://poser.pugx.org/laravel/framework/downloads.svg)](https://packagist.org/packages/laravel/framework)
[![Latest Stable Version](https://poser.pugx.org/laravel/framework/v/stable.svg)](https://packagist.org/packages/laravel/framework)
[![Latest Unstable Version](https://poser.pugx.org/laravel/framework/v/unstable.svg)](https://packagist.org/packages/laravel/framework)
[![License](https://poser.pugx.org/laravel/framework/license.svg)](https://packagist.org/packages/laravel/framework)
This is a simple Laravel app for a blog website to demonstrate key features of this web framework

Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable, creative experience to be truly fulfilling. Laravel attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as authentication, routing, sessions, and caching.
Companion slides: https://www.dropbox.com/s/nmi8sryhnl8voqo/eouyang_cs50_laravel-seminar_slides.pdf?dl=0

Laravel aims to make the development process a pleasing one for the developer without sacrificing application functionality. Happy developers make the best code. To this end, we've attempted to combine the very best of what we have seen in other web frameworks, including frameworks implemented in other languages, such as Ruby on Rails, ASP.NET MVC, and Sinatra.
It is recommended that you run this example on the CS50 Appliance

Laravel is accessible, yet powerful, providing powerful tools needed for large, robust applications. A superb inversion of control container, expressive migration system, and tightly integrated unit testing support give you the tools you need to build any application with which you are tasked.
### Setup

## Official Documentation
1. Install composer (for dependency management)

Documentation for the entire framework can be found on the [Laravel website](http://laravel.com/docs).
`curl -sS https://getcomposer.org/installer | php`

### Contributing To Laravel
`sudo mv composer.phar /usr/local/bin/composer`

**All issues and pull requests should be filed on the [laravel/framework](http://github.com/laravel/framework) repository.**
2. Download the repository.

### License
Distribution code: `git clone https://github.com/ericouyang/blog50.git`

The Laravel framework is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
Completed example: `git clone -b completed https://github.com/ericouyang/blog50.git`

3. Navigate into the new `blog50` folder and install dependencies

`composer install`

4. If you're running the completed example, you need to create the database called `blog50` and then run

`php artisan migrate`

5. Run the server using

`php artisan serve`

6. Go to this in your browser

`http://localhost:8000`