H
H
HeartOfProgrammer2016-02-26 20:59:10
Laravel
HeartOfProgrammer, 2016-02-26 20:59:10

Why doesn't laravel see a function in the controller?

There is a controller BookController
In which the following functions are
index(),create(),store(),show(),edit(),update(),destroy().
All of these functions work except for the test() function. I don't understand where the error could be.
Here is the entire code of my BookController controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class BookController extends Controller
{
    public function index()
    {
        $allBooks = Book::all();

        return view('books.booklist', compact('allBooks'));
    }

    public function create()
    {
        return view('books.addbook');
    }


    public function store(Requests\PublishBookRequest $requestData)
    {
        $book = new Book;
        $book->title = $requestData['title'];
        $book->description = $requestData['description'];
        $book->author = $requestData['author'];
        $book->save();

        return redirect()->route('book.index');
    }

    public function show($id)
    {
        $book = Book::find($id);

        return view('books.showbook')->with('book', $book);
    }

    public function edit($id)
    {
        $book = Book::find($id);

        return view('books.editbook')->with('book', $book);
    }

    public function test(){
        return 'hello!';
    }

    public function update($id, PublishBookRequest $requestData)
    {
        $book->title = $requestData['title'];
        $book->description = $requestData['description'];
        $book->author = $requestData['author'];
        $book->save();

        return redirect()->route('book.index');

    }

    public function destroy($id)
    {
        Book::find($id)->delete();

        return redirect()->route('book.index');
    }
}

My routes:
Route::resource('book', 'BookController');
Route::get('book', '[email protected]');
Route::get('book/create', '[email protected]');
Route::post('book', '[email protected]');
Route::get('book/{book}/edit', '[email protected]');
Route::put('book/{book}', '[email protected]');
Route::delete('book/{book}', '[email protected]');
Route::get('book/test', '[email protected]');

I feel I am not correctly specifying the URL in routes, if so, how should I enter the URL correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-02-26
@HeartOfProgrammer

Route::get('book/test', '[email protected]');
This must be specified before the resource, because the url 'book/test' falls under the get('book/{book}') rule.

Route::get('book', '[email protected]');
Route::get('book/create', '[email protected]');
Route::post('book', '[email protected]');
Route::get('book/{book}/edit', '[email protected]');
Route::put('book/{book}', '[email protected]');
Route::delete('book/{book}', '[email protected]');

Well, this is all just superfluous, since the resource is declared.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question