-3

What I want to set up in .\routes\web.php is something like

Route::resource('/main', CallController::class);

So, if user goes to www.sitename.com/, the index of CallController should be executed and displayed. However, if I try to compile that with npx vite build, I am invariably met with an error of such type:

Expected identifier but found ":"
216 |   * @route '/{}'
217 |   */
218 |  export const show = (args: { : string | number } | [param: string | number ] | string | number, options?: RouteQueryOptions): RouteDefinition<'get'> => ({
    |                               ^
219 |      url: show.url(args, options),
220 |      method: 'get',

The error only seems to go away, if some other path other that / is being assigned to as the path for the CallController's index.

The only workaround that I have found is to set the resource route as /calls and redirect from it from `/':

Route::get('/', function () {
        return redirect('/main');
    })->name('home');

But I wonder, whether it is the intended behaviour and if it is possible to use / as a route for a resource/controller?

0

1 Answer 1

1

If I'm not wrong that error is from TypeScript.

What’s missing there is a valid route parameter name. When you create a resource route with '/' like this...

Route::resource('/', CallController::class);

Laravel internally tries to create all the resource routes (show, edit, update, and destroy) and since the resource name is empty it ends up generating invalid placeholders like {}. You can confirm it by running php artisan route:list.

Take a look at how it work internally in ResourceRegistrar class. When the resource name is empty, the $base parameter is empty which is why Laravel generates these invalid {} routes.

You can either do what you're already doing, or register each route manually like:

Route::get('/', [CallController::class, 'index'])->name('main.index');
Route::resource('main', CallController::class)->except(['index']);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.