<?php

namespace Tightenco\Ziggy;

use Illuminate\Routing\Router;

class BladeRouteGenerator
{
    private $router;
    public $routes;

    public function __construct(Router $router)
    {
        $this->router = $router;
    }

    public function generate()
    {
        $json = (string) $this->nameKeyedRoutes();
        $appUrl = rtrim(config('app.url'), '/') . '/';

        return <<<EOT
<script type="text/javascript">
    var namedRoutes = JSON.parse('$json'),
        baseUrl = '$appUrl';

    function route (name, params, absolute = true) {
        return (absolute ? baseUrl : '') + namedRoutes[name].uri.replace(
            /\{([^}]+)\}/,
            function (tag) {
                return params[tag.replace(/\{|\}/gi, '')];
            }
        );
    }
</script>
EOT;
    }

    public function nameKeyedRoutes()
    {
        return collect($this->router->getRoutes()->getRoutesByName())
            ->map(function ($route) {
                return collect($route)->only(['uri', 'methods']);
            });
    }
}

<?php

namespace Tightenco\Ziggy;

use Illuminate\Routing\Router;

class BladeRouteGenerator
{
    private $router;
    public $routes;

    public function __construct(Router $router)
    {
        $this->router = $router;
    }

    public function generate()
    {
        $json = (string) $this->nameKeyedRoutes();
        $appUrl = rtrim(config('app.url'), '/') . '/';

        return <<<EOT
<script type="text/javascript">
    var namedRoutes = JSON.parse('$json'),
        baseUrl = '$appUrl';

    function route (name, params, absolute = true) {
        var domain = namedRoutes[name].domain ? namedRoutes[name].domain : baseUrl;
        var url = (absolute ? domain : '') + namedRoutes[name].uri

        return url.replace(
            /\{([^}]+)\}/gi,
            function (tag) {
                var key = tag.replace(/\{|\}/gi, '');
                if (params[key] === undefined) {
                    console.error('Ziggy Error: "' + key + '" key is required for route "' + name + '"');
                    return tag;
                }
                return params[key];
            }
        );
    }
</script>
EOT;
    }

    public function nameKeyedRoutes()
    {
        return collect($this->router->getRoutes()->getRoutesByName())
            ->map(function ($route) {
                return collect($route)->only(['uri', 'methods'])
                    ->put('domain', $route->domain());
            });
    }
}

Add domain route list to support subdomain routes; fix bug with multiple params; add console error for missing params.
Add domain route list to support subdomain routes; fix bug with multiple params; add console error for missing params.
