Support running in subdirectory with base_path config

This commit is contained in:
Toby Zerner 2015-08-13 12:58:59 +09:30
parent 7040ad1344
commit 0e1948cd3a
5 changed files with 13 additions and 5 deletions

View File

@ -45,8 +45,13 @@ export default function boot(app) {
app.modal = m.mount(document.getElementById('modal'), ModalManager.component());
app.alerts = m.mount(document.getElementById('alerts'), AlertManager.component());
const basePath = app.forum.attribute('basePath');
m.route.mode = 'pathname';
m.route(document.getElementById('content'), '/', mapRoutes(app.routes));
m.route(
document.getElementById('content'),
basePath + '/',
mapRoutes(app.routes, basePath)
);
m.endComputation();

View File

@ -254,8 +254,9 @@ export default class App {
route(name, params = {}) {
const url = this.routes[name].path.replace(/:([^\/]+)/g, (m, key) => extract(params, key));
const queryString = m.route.buildQueryString(params);
const prefix = m.route.mode === 'pathname' ? app.forum.attribute('basePath') : '';
return url + (queryString ? '?' + queryString : '');
return prefix + url + (queryString ? '?' + queryString : '');
}
/**

View File

@ -4,9 +4,10 @@
*
* @see https://lhorie.github.io/mithril/mithril.route.html#defining-routes
* @param {Object} routes
* @param {String} [basePath]
* @return {Object}
*/
export default function mapRoutes(routes) {
export default function mapRoutes(routes, basePath = '') {
const map = {};
for (const key in routes) {
@ -14,7 +15,7 @@ export default function mapRoutes(routes) {
if (route.component) route.component.props.routeName = key;
map[route.path] = route.component;
map[basePath + route.path] = route.component;
}
return map;

View File

@ -1,7 +1,7 @@
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400italic,700italic,400,700,600);
@import "font-awesome.less";
@fa-font-path: "/assets/fonts";
@fa-font-path: "../../assets/fonts";
@import "normalize.less";
@import "print.less";

View File

@ -25,6 +25,7 @@ class ForumSerializer extends Serializer
$attributes = [
'title' => Core::config('forum_title'),
'baseUrl' => Core::config('base_url'),
'basePath' => Core::config('base_path'),
'apiUrl' => Core::config('api_url'),
'welcomeTitle' => Core::config('welcome_title'),
'welcomeMessage' => Core::config('welcome_message'),