Home  >  Article  >  Backend Development  >  How to Extend Default Routes in Laravel Resource Controllers?

How to Extend Default Routes in Laravel Resource Controllers?

DDD
DDDOriginal
2024-10-27 17:51:02794browse

How to Extend Default Routes in Laravel Resource Controllers?

Extending Default Routes in Laravel Resource Controllers

By default, Laravel resource controllers provide a set of actions (index, create, store, edit, update, destroy). However, you may encounter scenarios where additional methods and routes are required.

To achieve this, register your custom route before defining the resource route. For instance:

<code class="php">Route::get('foo/bar', 'FooController@bar');
Route::resource('foo', 'FooController');</code>

Here's an example where a bar method is added to the FooController:

<code class="php">class FooController extends Controller
{
    // Custom method
    public function bar()
    {
        // Custom logic
    }

    // Default resource methods
    // ... (index, create, store, edit, update, destroy)
}</code>

By following these steps, you can seamlessly extend the functionality of Laravel resource controllers with additional custom methods and routes.

The above is the detailed content of How to Extend Default Routes in Laravel Resource Controllers?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn