dougu4704 2017-05-25 23:01
浏览 257
已采纳

在PHP框架中使用React。 我如何进行路由?

I have a website written using Kohana Framework. The way I've been doing things in my controller is like so:

public function action_about() {
    $this->template->body = View::factory('main');
    $this->template->body->set('main', 'about');
}

I've gone through a React tutorial where react router is used. To go from page to page, I had a function like so:

goToAbout(event) {
    event.preventDefault();
    this.context.router.transitionTo('about');
}

The way I understand it, if I'm adding React to an MVC framework, it should simply replace the V. That would mean I should still be using my framework's routing and controller. This is where I'm stuck. What am I missing?

  • 写回答

1条回答 默认 最新

  • douqinlin5094 2017-05-25 23:19
    关注

    In this circumstance, it augments the "View" layer of your application. You still have to render a view with your server side framework, but you can let React take care of everything after that. For example, suppose your react app has three routes "/users", "/users/:id", "/users/new". The routing in your server side framework (Kohana) must still respond to these routes.

    However, instead of rendering a custom view for each route, you would render out the same view in each controller - a view that includes the Javascript to mount your React app.

    Once your React app is mounted to the page, it will take care of parsing the URL and mounting the correct component.

    PS. Alternatively, if your server side framework allows it, you might also map each URL to the same controller to render the aforementioned view.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog