Laravel Classroom image

PRADEEP  JOSHI / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5 | 6 | 7 | 8|Last
Lessons:-The Blog Tutorial

1 <?php
2
3 Route::get('admin', array('before' => 'auth', 'do' => function() {
4 $user = Auth::user();
5 return View::make('pages.new')->with('user', $user);
6 }));
Now that we have a handle on our currently logged in user, let’s add that information to the
create new post view to identify our author. A hidden field should do the trick.
1 <?php
2
3 ...
4
5 {{ Form::open('login') }}
6
7 <!-- author -->
8 {{ Form::hidden('author_id', $user->id) }}
9
10 <!-- title field -->
11 <p>{{ Form::label('title', 'Title') }}</p>
12 <p>{{ Form::text('title') }}</p>
13
14 ...
We can now identify our author, and the create new post page is ready. We don’t need to link
to it, we want it hidden. We can simply hit the URL /admin if we want to create a new post.
Let’s handle a new post creation.
1 <?php
2
3 Route::post('admin', function() {
4
5 // let's get the new post from the POST data
6 // this is much safer than using mass assignment
7 $new_post = array(
8 'title' => Input::get('title'),
9 'body' => Input::get('body'),
10 'author_id' => Input::get('author_id')
11 );
12
13 // let's setup some rules for our new data
14 // I'm sure you can come up with better ones
15 $rules = array(
16 'title' => 'required|min:3|max:128',17 'body' => 'required'
18 );
19
20 // make the validator
21 $v = Validator::make($new_post, $rules);
22
23 if ( $v->fails() )
24 {
25 // redirect back to the form with
26 // errors, input and our currently
27 // logged in user
28 return Redirect::to('admin')
29 ->with('user', Auth::user())
30 ->with_errors($v)
31 ->with_input();
32 }
33
34 // create the new post
35 $post = new Post($new_post);
36 $post->save();
37
38 // redirect to viewing our new post
39 return Redirect::to('view/'.$post->id);
40
41 });
Now we should be able to create some blog posts, go ahead! Write a few articles so we have
something to view in our list all posts view.
Speaking of which, let’s get to work on that.
1 <?php
2
3 Route::get('/', function() {
4 // lets get our posts and eager load the
5 // author
6 $posts = Post::with('author')->all();
7
8 return View::make('pages.home')
9 ->with('posts', $posts);
10 });
We also need a view to list all of our blog posts, here we go..
pages/home.blade.php1 <?php
2
3 @layout('templates.main')
4
5 @section('content')
6 @foreach ($posts as $post)
7 <div class="post">
8 <h1>{{ HTML::link('view/'.$post->id, $post->title) }}</h1>
9 <p>{{ substr($post->body,0, 120).' [..]' }}</p>
10 <p>{{ HTML::link('view/'.$post->id, 'Read more &rarr;') }}</p>
11 </div>
12 @endforeach
13 @endsection
Finally we need a full view for our blog posts, let’s call it..
pages/full.blade.php
1 <?php
2
3 @layout('templates.main')
4
5 @section('content')
6 <div class="post">
7 <h1>{{ HTML::link('view/'.$post->id, $post->title) }}</h1>
8 <p>{{ $post->body }}</p>
9 <p>{{ HTML::link('/', '&larr; Back to index.') }}</p>
10 </div>
11 @endsection
We also need to add a new route to enable the view..
1 Route::get('view/(:num)', function($post) {
2 $post = Post::find($post);
3 return View::make('pages.full')
4 ->with('post', $post);
5 });
Now we have a fully working blog, with just the basics. Let’s take a quick look at what we could
do to improve it. Go ahead and try to add some of these features to test your Laravel skills.

 

 

 
 
 
image
PRADEEP  JOSHI

Skills    Laravel

Qualifications :-
Location :-Yamuna Colony,Dehradun,Uttarakhand,India
Description:-
Explore
 

  Students (0)