E
E
Eugene2022-04-20 15:40:18
React
Eugene, 2022-04-20 15:40:18

How to properly use React Router in Laravel?

The React app is right in the Laravel project, I use Laravel Mix to make sure everything works fine.
If you enter the site from the main page and follow the links, then everything works fine, but if you go to the same address, not the link from the site, but, for example, by entering the address manually, then a Laravel error 404 is displayed.
PS:
Found somewhere the solution to the problem, now when you go to an address that exists in react, but is missing in Laravel, react is displayed, but its main page.
Here is the web.php file code

Route::get('/{path?}', [
   'uses' => 'App\Http\Controllers\[email protected]',
   'as' => 'react-router.index',
    'where' => ['path' => '.*'],
]);

Here is the controller:
class ReactRouterController extends Controller
{
    public function index()
    {
        return view('welcome');
    }
}

I want to make it so that when switching to a page that is not the main one, the desired component is displayed, and not the main page

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question