aaronlord / laroute Goto Github PK
View Code? Open in Web Editor NEWGenerate Laravel route URLs from JavaScript.
License: MIT License
Generate Laravel route URLs from JavaScript.
License: MIT License
Hey,
One last thing here which is weird. The error above is being called everytime I try and add something like {{ laroute.action('HomeController@getIndex') }} in my template navigation in the header.
Sorry to throw up this :(
Cheers,
Mark
I see you have a 5.2 tag that was created by mistake I believe, it's currently making Composer not pick up the latest release (because for him 5.2 is the latest), should that tag be removed maybe?
Most developers will probably look up all URLs either by route or by controller action. For that reason, including both lookup methods in the compiled package is a big waste of space and runtime resources. An option for "route names only", "actions only", or both, would be great. Only the appropriate access methods should be included as well.
I would help with this, but... I see issues and pull requests going back years even though the package is also under active development, I would want to make sure there is intent to clear up the PRs and it will actually be considered before I spend time on this.
It would be nice to be to defined route groups, each group would get exported to a different file.
I'd personally find it handy since it wouldn't expose urls i use in the admin to the general users.
Config
Add a groups array, the keys in that array would be the group names, while the value would map to the filename/location
Route definition
Map default group:
'laroute' => true
Map to a defined group:
'laroute' =>'groupName'
Doing it this way would be backwards-compatible.
If you need help implementing this I'm willing to submit a PR
Even a simple laroute.link_to('hello');
doesn't work (laroute.js:25 Uncaught TypeError: parameters.join is not a function(…)
) because it calls this.route
with one argument and so the second argument (parameters
) is filled with wrong default ({}
) which doesn't have join
method since it's expected to be an array.
If app sits in subdirectory ie: domain.com/app/api/get-users (where /app/ is the main public dir then routes are generated incorrectly pointing to domain.com/api/get-users. Generator does not look up for routes in subdirectories like usual laravel router.
I am using deployer to deploy my site to production. One of the tasks I defined was as follows
task('artisan:laroute:generate', function () {
run('{{bin/php}} {{release_path}}/artisan laroute:generate');
});
However I get the following error when deploying:
Executing task artisan:laroute:generate
[mysite.com] > /usr/local/bin/php ~/releases/12/artisan laroute:generate
[mysite.com] < stdin: is not a tty
[mysite.com] < File does not exist at path vendor/lord/laroute/src/templates/laroute.js
When running the command manually on the server it works.
Any ideas ?
i want to get https
URL when i call route method. but it always return http
one.. i tried both absolute
true
and false
. but didn't work.. my rootUrl
is also have https...
Note: my site only works in https
, http
one always redirect to something not related to this project(i have added it, as i want. don't think about that :D )
current config...
var routes = {
absolute: true,
rootUrl: 'https://sub.example.com',
....
I am trying to install the package via composer. but received an error
don't install illuminate/filesystem v5.7.9|don't install laravel/framework v6.13.1
Installation request for laravel/framework (locked at v6.13.1, required as ^6.2) -> satisfiable by laravel/framework[v6.13.1].
I want to link to another route on another subdomain. This is not possible at this time am i correct?
Example routes.php:
Route::group(['domain' =>'sub2.' . Config::get('app.host')], function(){
Route::get('something/{id}', "OtherSubController@getSomething")->name('subdomain.route');
});
Example usage:
var myNewRoute = laroute.route('subdomain.route', {id: 3});
I would like to see this response:
"https://sub2.example.com/something/3"
Able to get success message but config file is not published.
Hi @aaronlord,
Very nice add-on for everything. But I can not draw connections according to dynamic language. How can I do that? If it is not, are you going to make an update?
I try install laroute to fresh laravel 5.1.1:
> composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Conclusion: remove laravel/framework v5.1.1
- Installation request for lord/laroute 2.* -> satisfiable by lord/laroute[v2.0.0].
- Conclusion: don't install laravel/framework v5.1.1
- Conclusion: don't install laravel/framework v5.1.0
- lord/laroute v2.0.0 requires illuminate/filesystem 5.0.* -> satisfiable by illuminate/filesystem[5.0.x-dev, v5.0.0, v5.0.22, v5.0.25, v5.0.26, v5.0.28, v5.0.33, v5.0.4].
- don't install illuminate/filesystem 5.0.x-dev|don't install laravel/framework 5.1.x-dev
- don't install illuminate/filesystem v5.0.0|don't install laravel/framework 5.1.x-dev
- don't install illuminate/filesystem v5.0.22|don't install laravel/framework 5.1.x-dev
- don't install illuminate/filesystem v5.0.25|don't install laravel/framework 5.1.x-dev
- don't install illuminate/filesystem v5.0.26|don't install laravel/framework 5.1.x-dev
- don't install illuminate/filesystem v5.0.28|don't install laravel/framework 5.1.x-dev
- don't install illuminate/filesystem v5.0.33|don't install laravel/framework 5.1.x-dev
- don't install illuminate/filesystem v5.0.4|don't install laravel/framework 5.1.x-dev
- Installation request for laravel/framework 5.1.* -> satisfiable by laravel/framework[5.1.x-dev, v5.1.0, v5.1.1].
Please add laravel 5.8 as supported version if possible. Can't upgrade it because of the laroute please check https://pastebin.com/raw/vBn3Fs4A
thanks in advance.
Currently Laroute behaves the same way as the UrlGenerator, but there is one caveat. While in PHP you don't have to specify the name of the arguments, in Laroute you do:
URL::route('users.show', 3); // foo.com/users/3
laroute.route('users.show', {user: 3}); // foo.com/users/3
laroute.route('users.show', 3); // foo.com/users/{users}?3
Could this difference of behavior be fixed?
Hey,
Just setting up the package on L4 and when I go to do php artisan generate:laroute I get an error. It seems like it is conflicting with Jeffrey Way's Laravel Generators maybe? This is the error:
"[InvalidArgumentException]
Command "generate:laroute" is not defined."
Is this an error at my side?
I recently had a problem generating the js file, I had updated api.php but by running the php artisan laroute:generate
command I did not modify js. After a while I thought about clearing the cache of the Laravel routes and then everything worked correctly.
I suggest making the call $this->call('route:cache');
at the beginning of the function handle()
.
Is there a way to include only dingo api routes?
Is it intentional?
> laroute.route('search', {q: 'this & that'})
"/home/search?q=this & that"
The & that
is not part of q
anymore because &
is query string separator. It should be replaced with its percent encoding.
Laravel 5.5 introduced auto-discovery for Providers
of a package.
See here how to configure auto-discovery.
Laroute generate index in laravel route->controller should be empty, but it's returning "/index".
e.g:
$route->controller('admin', 'AdminController', [
'getIndex' => 'admin.index',
'getPayment' => 'admin.payment'
]);
The uri in getIndex is returning "admin/index", but the correct should be only "admin" if I don't pass any parameters in controller.
There are tests but they aren't run on every build, this would prevent syntax error bugs and stuff.
Currently, rootUrl
is compiled from APP_URL
when laroute:generate
is executed. As far as I can tell, rootUrl
is only ever used when laroute.absolute
is false
(default). Otherwise, it is not needed.
Using relative path generation assumes URIs are mounted under the domain name, not considering an application that might be hosted in a subdirectory of that domain. Laravel's UrlGenerator doesn't care much about this because it relies on Symfony's Request
to generate a correct URI up to the point it takes over. Using absolute URLs when generating is a way around this since JavaScript has no way of telling the difference between what's a subdirectory and what's route.
Here's my specific problem:
http://local.dev
.http://example.org/application
.laroute.js
file generated by this package. I do not bundle assets in production; thus, the only .env
available at compile-time is my local one.So, if I pack everything up locally and use absolute URLs, I end up with a production application attempting to query http://local.dev/generated/path
when in fact, I want http://example.org/application/generated/path
. Previous to discovering this package, I was doing something fairly similar: creating a JavaScript object populated with data from the backend. The difference was that I didn't need to support parameterized routes during prototyping and now I do, so I'm delighted to find this package! 😁
However, I would like to contribute a means of determining root URL at runtime, based on either the .env#APP_URL
configuration OR by generating it and disregarding what is in .env#APP_URL
.
All this to say, would you be interested in such a contribution and if so, do you have any specific constraints towards development? I've got a local patch that works for me but it requires something like this in my base template (which actually works alright and is reasonable, to me):
<script src="{{ asset("public/js/laroute.js") }}"></script>
<script>
laroute.config.rootUrl = "{{ env('APP_URL') }}"
</script>
Essentially, exposing configuration to userland to be changed at runtime, while also allowing compile-time generation from your published vendor configuration.
I pulled down the latest version of laroute and got an issue with invalid urls. I see that you have fixed this in your recent commits but have not pushed a new release for this yet. A new release with these fixes would be appreciated 👍
This is not an issue per se, but since Laravel includes Vue by default, it would be nice to have a laroute
Vue plugin available in this package. I have just written one and wanted to ask whether you are interested in including it as an additional template, defaulting to the current laroute.js implementation. You can find the gist right here: https://gist.github.com/Radiergummi/08152f9e6e1f87b57d7acc017a33fdd9
Currently, it does the following things:
$router
property to Vue instances that has the methods route()
, action()
, url()
, link_to()
, link_to_route()
, link_to_action()
and match()
availablerouter-link
component to Vue that is available globally and works much like the one from vue-router
I'm still working on it for my current project, but if you're interested in adding it to Laroute, I'd be happy to share the source once it's finished and tested.
With the help of a filter
option like the Laravel Javascript Routes package.
Can you just let us know if you wish to abandon this project so that someone can fork it and continue development? I realize that technically anyone can fork it at any time, but I'd prefer not to do this if you plan on coming back.
Thanks
Will there be support for Laravel 5.x?
Specifically, I want to disable this feature:
laroute.action('HomeController@getIndex');
I always use named routes. It's not necessary to expose my class structure. If not for security, then simply to reduce the output file size.
例如我的 路由名为 laroute('users/index') js会报错的
Request:
window.axios.post(laroute.route(this.loadRoute, {ids: idsArray}))
Error:
ids must be an array
Request:
array:1 [ "ids" => "46,50,5...3,74"]
// string
According to upgrade guide, fire
methods in console commands must be renamed to handle
. Please see #69
The filter method of laroute => false
is not working. I triple-checked. I have setted in configs 'filter' => 'all'
property, and at route groups / single routes I give an array with "laroute" => false
, generate a new javascript, but anyway...it includes even routes with "laroute => false"
value...
const params = { id: 1 }
Laroute.route(route, params)
console.log(params) // > {}
Hi, I've experienced a bug when by using route method and passing object with params to it, I get my object kinda destroyed. This issue is occurs in the replaceNamedParameters()
I believe.
Let's say we have this route
Route::get('/something', [
'laroute' => true,
'as' => 'something',
// Note the lowercase u from user@..., typically this value
// starts with an uppercase letter and the following problem won't apply
'uses' => 'user@someMethod'
]);
At some point the value of uses
from above will end up in an array
array:2 [
//...
"action" => "App\Http\Controllers\user@someMethod"
//...
]
Later on that piece will end up being json_encode
ed, which will generate a string containing
... App\\Http\\Controllers\\user@someMethod ...
When converted to json, the backslashes are escaped, as expected.
Ultimately, this json will end up being replaced in the template via preg_replace
, However, that double backslash \\
will end up being a single \
backslash afterwards
To use backslash in replacement, it must be doubled ("\\" PHP string). link
Thus, in the file generated from routes.js template, that json will look like
... App\Http\Controllers\user@someMethod ...
In this case, the \u
will be interpreted by Javascript as the start of a unicode escape sequence, and since it isn't followed by a hex number, it will trigger an error:
SyntaxError: malformed Unicode character escape sequence
Similar stuff might also happen with \b, \f, \n, \r, \t, \v
If it was up to me I would simply use a str_ireplace
instead, in TemplateCompiler, which would preserve the escaped backslashes until the end, but I am not sure if I'm missing some other reason why preg_replace
was used.
When the Laravel app is hosted at a subdirectory on the webserver and no prefix is set, the returned URL has a / prepended, forcing the URL to become absolute.
Line 85:
getCorrectUrl: function (uri) {
var url = this.prefix + '/' + uri.replace(/^\/?/, '');
I changed to:
getCorrectUrl: function (uri) {
if( this.prefix != "")
var url = this.prefix + '/' + uri.replace(/^\/?/, '');
else
var url = uri.replace(/^\/?/, '');
I'm certain that can be implemented better but it resolved the problem for me.
After generating the file and viewing the page I have spotted an error. An error is to do with an invalid Unicode escape sequence. After searching for the bug in the laroute.js file I spotted that JS was trying to scape "\u" in the action part of the route statement.
Whilst I have hardcoded the fix for myself - it might be of benefit to adjust getRouteInformation() method to handle any backslashes within an action.
/**
* Get the route information for a given route.
*
* @param $route \Illuminate\Routing\Route
* @param $filter string
* @param $namespace string
*
* @return array
*/
protected function getRouteInformation(Route $route, $filter, $namespace)
{
$host = $route->domain();
$methods = $route->methods();
$uri = $route->uri();
$name = $route->getName();
$action = $route->getActionName();
$laroute = array_get($route->getAction(), 'laroute', null);
if(!empty($namespace)) {
$a = $route->getAction();
if(isset($a['controller'])) {
$action = str_replace($namespace.'\\', '', $action);
}
}
// escape backslashes
$action = addslashes($action);
switch ($filter) {
case 'all':
if($laroute === false) return null;
break;
case 'only':
if($laroute !== true) return null;
break;
}
return compact('host', 'methods', 'uri', 'name', 'action');
}
I think there is a security risk. Can you specify to store certain routes?
There isn't a 2.* tag yet defined.
Currently it crashes if the folder where the .js should be doesn't exist:
[ErrorException]
file_put_contents(public/builds/js/routes.js): failed to open stream: No such file or directory
Hi, in Lord\Laroute\LarouteServiceProvider class line 64 in 'registerCompiler' method it should be 'Lord\Laroute\Compilers\TemplateCompiler' not 'Lord\Laroute\Compilers\Templatecompiler'.
(Note the capital C in TemplateCompiler)
Hi,
is it possible support laravel 5.3?
Best Regards
Peter
There is a typo in the toJson method call in the command.
It should be written "toJson" instead of "toJSON"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.