0

I try to create a react routing on my laravel application. But that doesn't work. I've followed several tutorials without any success.

First, I've add this route to route.php:

Route::get('/', function () {
return view('index', []); });

Then, in index.blade.php:

    <div id="app"></div>
    <script src="{{asset('js/app.js')}}" ></script>

Then, in my app.js, I've add the routes:

  <BrowserRouter>
    <Switch>
     <Route path="currency" component={Currency}/>
     <Route path="/" component={Index}/>

    </Switch>  </BrowserRouter>

When I reach http://localhost/project/public, the index component is OK but when I try to join http://localhost/project/currency, that doesn't work. I try to remove the route of index component but it's the same for currency.

Do you have an idea ?

Thank's a lot

4 Answers 4

2

Is "project" the base folder for the app?

Add baseName param to the BrowserRouter

    <BrowserRouter basename='/project'>

You also need to update the laravel route file to always go to the app index route if it matches the pattern .../project/... So /project and /project/currency both should be routed by laravel to index which is the app and the app handles the routing logic from there on. Had to do the same with my CakePHP project.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for you answer. I've tried to add the basename but that doesn't change anything.
You also need to update the laravel route file to always go to the app index route if it matches the pattern .../project/... So /project and /project/currency both should go to index which is the app and the app handles the routing logic from there on.
Ok. I've just tried to add: Route::get('/currency', function () { return view('index', []); }); But now it's alway the index component whose displayed
See 'Creat Wildcard route' section blog.pusher.com/react-laravel-application
I create this route: Route::get('/{path?}', [ 'uses' => 'ReactController@show', 'as' => 'react', 'where' => ['path' => '.*'] ]);
|
0

Open the Laravel router for response any request!

Route::get('{all?}', function(){
    return view('index');
})->where('all', '([A-z\d-\/_.]+)?');

Comments

0

routing from web.php would give the perfect routing for laravel and react, you can do any routing from laravel to retrieve data.... include

Route::view('/{path?}', 'index');

at end of web routes so that it will be loaded at last getting your all views

Comments

0

Add this in web.php in Route directory:

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

It will work perfectly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.