Hiện tại mình đang tạo một Form để add new blog.
Từ trang localhost:8888/blog/public/post
dẫn đến Form bằng đường dẫn localhost:8888/blog/public/post/create
. Nhưng khi mình gặp lỗi sau: Sorry, the page you are looking for could not be found.
Code ở file routes/web.php :Route::get('/post', 'PostController@index'); Route::get('/post/{id}', 'PostController@show'); Route::get('/post/create', 'PostController@create');
Ở file PostController :
class PostController extends Controller
{
public function index() {
$posts = Post::all() ;
return view('post.index', compact('posts')) ;
}
public function show(Post $id) {
return view('post.show',compact('id')) ;
}
public function create() {
return view('post.create') ;
}
}
Ở file index:
@extends ('post.layouts.master')
@section ('content')
<div class="container">
<div class="jumbotron">
<h1 style="text-align: center;">Welcome To My Blog</h1>
</div>
</div>
@if(count($posts) > 0)
<ul class="list-group">
@foreach ($posts as $post)
<li class="list-group-item"><a href="post/{{$post->id}}">{{$post->title}}</a></li>
@endforeach
</ul>
@endif
<h2 style="text-align: center;"><a href="post/create">Add new post</a></h2>
@endsection
Cây thư mục view:
Nếu mình sửa như thế này sẽ truy cập được Form:
File index:
<h2 style="text-align: center;"><a href="/create">Add new post</a></h2>
File route:
Route::get('/create', 'PostController@create');
Kết Quả:
Code đầu tiên mình làm theo tutorial ở Laracast nhưng chẳng hiểu sao lại bị báo lỗi, có ai giải thích giúp mình vì sao không?
Mình cám ơn trước