Routes and Content
A fundamental part of extending Flarum is adding routes — both to expose new resources in the JSON-API, and to add new pages to the frontend.
Routing happens on both the PHP backend and the JavaScript frontend.
Backend Routes
On the backend, Flarum has three collections of routes:
-
forumThese routes are accessible underyourforum.com/. They include routes that show pages in the frontend (likeyourforum.com/d/123-title) and other utility routes (like the reset password route). -
adminThese routes are accessible underyourforum.com/admin/. By default, there is only oneadminroute on the backend; the rest of the admin routing happens on the frontend. -
apiThese routes are accessible underyourforum.com/api/and make up Flarum's JSON:API.
Defining Routes
You can add routes to any of these collections using the Routes extender. Pass the name of the collection in the extender's constructor, then call its methods to add routes.
There are methods to register routes for any HTTP request method: get, post, put, patch, and delete. All of these methods accept three arguments:
$pathThe route path using FastRoute syntax.$nameA unique name for the route, used for generating URLs.$nameA unique name for the route, used for generating URLs. To avoid conflicts with other extensions, you should use your vendor name as a namespace.$handlerThe name of the controller class that will handle the request. This will be resolved through the container.
<?php
use Flarum\Extend;
use Acme\HelloWorld\HelloWorldController;
return [
(new Extend\Routes('forum'))
->get('/hello-world', 'acme.hello-world', HelloWorldController::class)
];
You can use the CLI to automatically generate your routes:
$ flarum-cli make backend route
Controllers
In Flarum, Controller is just another name for a class that implements RequestHandlerInterface. Put simply, a controller must implement a handle method which receives a Request and must return a Response. Flarum includes laminas-diactoros which contains Response implementations that you can return.
<?php
namespace Acme\HelloWorld;
use Laminas\Diactoros\Response\HtmlResponse;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
class HelloWorldController implements RequestHandlerInterface
{
public function handle(Request $request): Response
{
return new HtmlResponse('<h1>Hello, world!</h1>');
}
}
Controllers are resolved from the container so you can inject dependencies into their constructors.
The handle method of a Controller is the code that runs when someone visits your route (or sends data to it via a form submission). 一般而言,控制器实现的模式如下:
- 从请求对象中获取信息 (GET params, POST data, 当前用户等)。
- 使用该信息做一些事情。 Do something with that information. For instance, if our controller handles a route for creating posts, we'll want to save a new post object to the database.
- 返回响应。 Return a response. Most routes will return an HTML webpage, or a JSON api response.
路由参数
有时您需要抓取路由中的 URI 部分。 You may do so by defining route parameters using the FastRoute syntax:
(new Extend\Routes('forum'))
->get('/user/{id}', 'acme.user', UserController::class)
The values of these parameters will be merged with the request's query params, which you can access in your controller by calling $request->getQueryParams():
use Illuminate\Support\Arr;
$id = Arr::get($request->getQueryParams(), 'id');
生成 URL
You can generate URLs to any of the defined routes using the Flarum\Http\UrlGenerator class. Inject an instance of this into your controller or view, and call the to method to select a route collection. 然后,你可以使用你给它定义时给它的名称生成路径的 URL。您可以传递数组参数作为第二个参数。参数将填充匹配的 URI 部分,否则它们将作为查询参数添加。
$url = $this->url->to('forum')->route('acme.user', ['id' => 123, 'foo' => 'bar']);
// http://yourforum.com/user/123?foo=bar
视图
You can inject Laravel's View factory into your controller. This will allow you to render a Blade template into your controller's response.
First, you will need to tell the view factory where it can find your extension's view files by adding a View extender to extend.php:
use Flarum\Extend;
use Illuminate\Contracts\View\Factory;
return [
(new Extend\View)
->namespace('acme.hello-world', __DIR__.'/views');
];
Then, inject the factory into your controller and render your view into an HtmlResponse:
class HelloWorldController implements RequestHandlerInterface
{
protected $view;
public function __construct(Factory $view)
{
$this->view = $view;
}
public function handle(Request $request): Response
{
$view = $this->view->make('acme.hello-world::greeting');
return new HtmlResponse($view->render());
}
}