简体   繁体   中英

Naming routes different from Controller / Database

In a Laravel 5.8 app i want my url's to be in Dutch, however, for consistency and just general easier use, i want to name everything else by their English names. Eloquent will ( as far as i know ) only pass me the proper vars if my Model, Controller, table etc. are all named the same.

Right now, i have a route named /backend/facturen which in English would be /backend/invoices. I have tried using names routes, however, i found that this wasn't what i was looking for.

My route:

Route::resource('/backend/facturen', 'InvoicesController');

My show method inside the InvoicesController:

public function show(Invoice $invoice) {
    return view('backend.invoice.show', compact('invoice'));
}

The database table is named 'invoices'.

The only way so far i have got this to work is by renaming my route to:

Route::resource('/backend/invoices', 'InvoicesController');

But, of course, this is not a solution to my problem.

I would like to see all data from the invoices show up on my ( Dutch ) /facturen route.

Resource controllers by default look for a variable with the same name as the (last part of the) URL, so renaming the URL changes that variable as well.

But you can tell Laravel to use a different name :

Route::resource('/backend/facturen', 'InvoicesController')->parameters([
    'facturen' => 'invoice'
]);

You could also get the variable itself from the id, without using the laravel magic to correctly detecting the variable from the url:

public function show($id) {
    $invoice = Invoice::findOrFail($id);

    return view('backend.invoice.show', compact('invoice'));
}

No, you have registered a route pointing to the URL /backend/facturen , to name it, use the name function:

Route::resource('/backend/facturen', 'InvoicesController')->name('backend.invoice.show');

Even then, it won't show that route, when using the view function you need to pass the path to your template, not the route.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM