J
J
Jedi2019-02-06 20:05:22
Laravel
Jedi, 2019-02-06 20:05:22

How to solve Laravel + React routing issues?

Good evening!
Help solve the problem, please.
App.js Code

class App extends Component {
    render () {
        return (
            <BrowserRouter>
                <div>
                    <Header />
                    <Switch>
                        <Route exact path="/" component={Homepage}/>
                        <Route exact path="/cars" component={Cars}/>
                        <Route path="/cars/:id" component={Car}/>
                        <Route path="*" component={NotFound}/>
                    </Switch>
                </div>
            </BrowserRouter>
        )
    }
}

Routing code in Laravel (Web.php) Routing code in Laravel (Api.php)
Route::view('/{path?}', 'app');
Route::get('/', '[email protected]');

Route::get('/cars', '[email protected]');

Route::get('/cars/{car}', '[email protected]');

Everything works properly if you follow the links from the beginning. But the problem arises if you follow the link, for example: https://mywebchik.xuyax/cars/1 . The problem is that the server gives 404 (Not Found). :( Works if the link is fixed (I call it that) - https://mywebchik.xuyax/cars .
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Jedi, 2019-02-27
@PHPjedi

Fucking pussy! I found a solution!

// web.php

Route::get('/{view?}', function () {
    return view('app');
})->where('view', '(.*)');

B
Boris Korobkov, 2019-02-06
@BorisKorobkov

If the link https://mywebchik.xuyax/cars/1 after https://mywebchik.xuyax/cars works, but it doesn’t work without it, then this is not rotting, and something is wrong with your backend business logic. I suppose you pre-record something in the session?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question