diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 6a780c4..0000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,3 +0,0 @@ -# Contribution Guidelines - -Please submit all issues and pull requests to the [laravel/framework](http://github.com/laravel/framework) repository! diff --git a/app/controllers/BlogController.php b/app/controllers/BlogController.php index 421be11..114b662 100644 --- a/app/controllers/BlogController.php +++ b/app/controllers/BlogController.php @@ -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)); + } } diff --git a/app/database/migrations/2014_11_07_155735_create_posts_table.php b/app/database/migrations/2014_11_07_155735_create_posts_table.php index 36dd8d9..814f87b 100644 --- a/app/database/migrations/2014_11_07_155735_create_posts_table.php +++ b/app/database/migrations/2014_11_07_155735_create_posts_table.php @@ -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(); }); } @@ -28,7 +28,7 @@ public function up() */ public function down() { - Schema::drop('users'); + Schema::drop('posts'); } } diff --git a/app/database/migrations/2014_11_07_170248_create_comments_table.php b/app/database/migrations/2014_11_07_170248_create_comments_table.php index a386328..0a88a33 100644 --- a/app/database/migrations/2014_11_07_170248_create_comments_table.php +++ b/app/database/migrations/2014_11_07_170248_create_comments_table.php @@ -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(); }); } diff --git a/app/models/Comment.php b/app/models/Comment.php index 434ae92..65c7827 100644 --- a/app/models/Comment.php +++ b/app/models/Comment.php @@ -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'); } } diff --git a/app/models/Post.php b/app/models/Post.php index 26aa602..ec76430 100644 --- a/app/models/Post.php +++ b/app/models/Post.php @@ -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'; + } } diff --git a/app/routes.php b/app/routes.php index 7a9c042..ca72451 100644 --- a/app/routes.php +++ b/app/routes.php @@ -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' )); diff --git a/app/views/blog/partials/post.blade.php b/app/views/blog/partials/post.blade.php index 814a2d3..125a457 100644 --- a/app/views/blog/partials/post.blade.php +++ b/app/views/blog/partials/post.blade.php @@ -13,7 +13,8 @@ {{ str_limit($post->content, 430) }} @endif -