我目前在CRUD
内置了一个简单的Laravel
应用程序,与database
Books
进行交互。这是我的routes.php
文件:
Route::model('book', 'Book');
//Show pages
Route::get('/', 'BookController@index');
Route::get('book/create', 'BookController@create');
Route::get('book/edit/{book}', 'BookController@edit');
Route::get('book/delete/{book}', 'BookController@delete');
Route::get('book/view/{book}', 'BookController@view');
//Handle form submissions
Route::post('create', 'BookController@handleCreate');
Route::post('edit', 'BookController@handleEdit');
Route::post('delete', 'BookController@handleDelete');
然后在我的views
文件夹中,我有以下文件:
create.blade.php
edit.blade.php
index.blade.php
layout.blade.php
view.blade.php
Controller
为BookController
:
class BookController extends BaseController {
//View page - Shows list of books
public function index(){
$books = Book::all();
//compact()creates array of the books using names as keys
return View::make('index', compact('books'));
}
//Create page - Shows create Form
public function create(){
return View::make('create');
}
//Logic to add a new book to the DB
public function handleCreate(){
$book = new Book;
$book->book_name = Input::get('name');
$book->book_desc = Input::get('desc');
$book->save();
return Redirect::action('BookController@index');
}
//Edit page - Shows edit form and populates with book info
public function edit(Book $book){
return View::make('edit', compact('book'));
}
//Logic to update an existing book in the DB
public function handleEdit(){
$book = Book::findOrFail(Input::get('id'));
$book->book_name = Input::get('name');
$book->book_desc = Input::get('desc');
$book->save();
return Redirect::action('BookController@index');
}
//Delete page
public function delete(Book $book){
//Show delete confirmation page
return View::make('delete', compact('book'));
}
public function handleDelete(){
//Handle delete logic...
$id = Input::get('book');
$book = Book::findOrFail($id);
$book->delete();
return Redirect::action('BookController@index');
}
//View
public function view(Book $book){
return View::make('view', compact('book'));
}
}
我的问题是如果我将不同的views
嵌套在名为books
的子文件夹中,我该如何修改我的路线。我尝试创建一个名为books
的文件夹并将views
粘贴到其中,当我运行应用程序时,它说它无法找到视图。
答案 0 :(得分:1)
您可以使用点语法向下浏览views文件夹中的文件夹结构。以您的索引视图为例,如果您希望它存在views\books
,您可以这样做:
public function index(){
$books = Book::all();
return View::make('books.index', compact('books'));
}
那就是它! Laravel非常简单。
因此,如果我们想要更深入地了解,books
内的另一个名为travel
的子文件夹会为您提供views\books\travel
:
public function index(){
$books = Book::all();
return View::make('books.travel.index', compact('books'));
}
同样值得注意的是,Laravel使用BookController
轻松路由到Route::resource
这样的RESTful控制器。所有映射GET和POST请求都是为您完成的。请在此处查看:http://laravel.com/docs/controllers#resource-controllers