1
0
mirror of https://github.com/flarum/core.git synced 2025-08-17 22:01:44 +02:00

Roughly implement routes and data preloading

Only preloading data for basic requests w/o query params, at least for
the moment - if we have to preload for something like
/?q=test&sort=newest, we end up having to duplicate a whole lot of
logic between JS/PHP.
This commit is contained in:
Toby Zerner
2015-06-18 17:41:37 +09:30
parent c2da4a946f
commit 822a216cc9
11 changed files with 126 additions and 26 deletions

View File

@@ -100,7 +100,7 @@ class IndexAction extends SerializeCollectionAction
static::addPaginationLinks(
$document,
$request,
$this->url->toRoute('flarum.api.discussions.index'),
$request->http ? $this->url->toRoute('flarum.api.discussions.index') : '',
$results->areMoreResults()
);

View File

@@ -32,8 +32,8 @@ class ShowAction extends SerializeResourceAction
public static $include = [
'startUser' => false,
'lastUser' => false,
'startPost' => true,
'lastPost' => true,
'startPost' => false,
'lastPost' => false,
'posts' => true,
'posts.user' => true,
'posts.user.groups' => true,

View File

@@ -0,0 +1,18 @@
<?php namespace Flarum\Forum\Actions;
class DiscussionAction extends IndexAction
{
protected function getDetails($request, $params)
{
$response = $this->apiClient->send('Flarum\Api\Actions\Discussions\ShowAction', [
'id' => $params['id'],
'near' => $params['near']
]);
// TODO: return an object instead of an array?
return [
'title' => $response->data->title,
'response' => $response
];
}
}

View File

@@ -6,6 +6,7 @@ use Flarum\Assets\JsCompiler;
use Flarum\Assets\LessCompiler;
use Flarum\Core;
use Flarum\Forum\Events\RenderView;
use Flarum\Forum\Loaders\LoaderInterface;
use Flarum\Locale\JsCompiler as LocaleJsCompiler;
use Flarum\Support\Actor;
use Flarum\Support\HtmlAction;
@@ -66,11 +67,18 @@ class IndexAction extends HtmlAction
}
}
$details = $this->getDetails($request, $params);
$data = array_merge($data, array_get($details, 'data', []));
$response = array_get($details, 'response');
$title = array_get($details, 'title');
$view = view('flarum.forum::index')
->with('title', Core::config('forum_title'))
->with('title', ($title ? $title.' - ' : '').Core::config('forum_title'))
->with('config', $config)
->with('layout', 'flarum.forum::forum')
->with('data', $data)
->with('response', $response)
->with('session', $session)
->with('alert', $alert);
@@ -112,6 +120,23 @@ class IndexAction extends HtmlAction
->with('scripts', [$assets->getJsFile(), $localeCompiler->getFile()]);
}
protected function getDetails($request, $params)
{
$queryParams = $request->getQueryParams();
// Only preload data if we're viewing the default index with no filters,
// otherwise we have to do all kinds of crazy stuff
if (!count($queryParams) && $request->getUri()->getPath() === '/') {
$response = $this->apiClient->send('Flarum\Api\Actions\Discussions\IndexAction');
return [
'response' => $response
];
}
return [];
}
protected static function filterTranslations($translations)
{
$filtered = [];

View File

@@ -2,7 +2,6 @@
use Flarum\Http\RouteCollection;
use Flarum\Http\UrlGenerator;
use Flarum\Support\AssetManager;
use Flarum\Support\ServiceProvider;
use Psr\Http\Message\ServerRequestInterface;
@@ -49,16 +48,36 @@ class ForumServiceProvider extends ServiceProvider
{
$this->app->instance('flarum.forum.routes', $routes = new RouteCollection);
/**
* Route::group(['middleware' => 'Flarum\Forum\Middleware\LoginWithCookie'], function () use ($action) {
* For the two below
*/
$routes->get(
'/',
'flarum.forum.index',
$this->action('Flarum\Forum\Actions\IndexAction')
);
$routes->get(
'/d/{id:\d+}/{slug}',
'flarum.forum.discussion',
$this->action('Flarum\Forum\Actions\DiscussionAction')
);
$routes->get(
'/d/{id:\d+}/{slug}/{near}',
'flarum.forum.discussion.near',
$this->action('Flarum\Forum\Actions\DiscussionAction')
);
$routes->get(
'/u/{username}',
'flarum.forum.user',
$this->action('Flarum\Forum\Actions\IndexAction')
);
$routes->get(
'/settings',
'flarum.forum.settings',
$this->action('Flarum\Forum\Actions\IndexAction')
);
$routes->get(
'/logout',
'flarum.forum.logout',