As you know Laravel4 omits the trailing slashes from all URLS. I've Laravel4 X AngularJS SPA (Single Page Application), and simply my current URLs looks like this:

http://localhost/en#/nglink

What I'd like to achieve is to make links looks like this:

http://localhost/en/#/nglink

So as you can see, I need a prefix slash before the AngularJS links (#/nglink), or a trailing slash after Laravel's links (http:// localhost/en). Is there anyway to achieve this using AngularJS? If not how to achieve it without editing Laravel's core files?

有帮助吗?

解决方案

Well, it's possible to achieve that though either AngularJS or Laravel or http server side, but it's better & easier to be done through Laravel itself since we can just override the required classes (URLGenerator mainly) without touching core files, and while keeping the code (server agnostic), so it could work with apache/nginx or any other server with trailing slash (that's why I preferred not to work with htaccess).

Update #1

Laravel 5 / AppServiceProvider::register()

    $this->app->bindShared('url', function ($app) {
        $routes = $app['router']->getRoutes();

        $request = $app->rebinding('request', function ($app, $request) {
            $app['url']->setRequest($request);
        });

        // This is your custom overridden "UrlGenerator" class
        $urlGenerator = new UrlGenerator($routes, $request);

        return $urlGenerator;
    });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top