Using pound sign (#) in Laravel routing? - laravel

Laravel has no problem routing the following URI:
$router->get('demo/toggle.html', function() {
return View::make('ng.demo.toggle');
});
However, this one won't work for some reason.
$router->get('demo#/toggle.html', function() {
return View::make('ng.demo.toggle');
});
Is there a way to make this work?

Everything behind the hashtag (#) isn't send to the server, so Laravel can't catch it when you enter it in the browser. This is where the error comes from, Laravel only gets demo.
You can try this with an existing, working route. Just write
demo/toggle.html#some_gibberish <<< will still take you to demo/toggle.html
I'm wondering why you are using '..../toggle.html' as getter, one of the benefits of (Laravel's) url rewriting is that this is avoidable. You could use only toggle instead.

Related

Route::redirect with wildcard in Laravel 5.5+

The new Route::redirect introduced in Laravel 5.5 is practical, but does it allow {any} wildcard?
Here is what I used to do in Laravel 5.4
Route::get('slug', function () {
return redirect('new-slug', 301);
});
In Laravel 5.5, I can do the following:
Route::redirect('slug', url('new-slug'), 301);
Which allows route caching by getting rid of the closure.
So far so good, but what if I want to use a wildcard? In Laravel 5.4, I could do:
Route::get('slug/{any}', function ($any) {
return redirect('new-slug/'.$any, 301);
});
Of course, I can still use this in Laravel 5.5, but my point is to be able to cache my route files.
Does the new Route::redirect allow the use of a wildcard, or is my only option to use a controller?
EDIT: What I am attempting to do is something like this:
Route::redirect('slug/{any}', url('new-slug/'.$any), 301);
Which of course doesn't work because I don't know where to reference the $any variable.
You may use:
Route::redirect('slug/{any}', url('new-slug', Request::segment(2)), 301);
If you need to redirect with input data:
Route::redirect('slug/{any}', str_replace_first('slug', 'new-slug', Request::fullUrl()), 301);
Note that the above functions url() Request::segment(2) str_replace_first will be called in each request although there is no match for slug/{any}, nothing to worry about but I prefer to create my own controller in this case or add the redirect in the web server directly.

Best way to pass url as parameter to route

I am trying to pass any full url in as parameter to route, but the slash seems to mess everything up. If a route is passed in encoded, the route seems to decode it, is there a way to stop this or encode the url again at route level?
Route::get('add/{title?}/{url?}', 'HomeController#add')->name('add');
also i have tired
Route::get('add/{title?}/{url?}', 'HomeController#add_')->where('url', '(.*)')->name('add_popup');
but if it comes across question mark in the url it will drop anything after the question mark.
Check this out
Route::get("url/{url}", function($url) {
return $url;
})->where('url', '.*');
Example: http://myapp.test/url/http://try.me.com prints
http://try.me.com
I would recommend Jeunes answer, however if you still want a route parameter you could do a base64 encode/decode. This will not make for a pretty url though.
JS
btoa("http://someurl.test")
PHP
Route::get('url/{url}', function ($url) {
return base64_decode($url);
});

angular 2 route to 404 page when route param is invalid

Say I have an route with a param like this (in Angular 2): /user1/products/:id, which could have child routes like /user1/products/3/reviews.
When I navigate to /user1/products/-1, I check with my server and if the product doesn't exist, I want to render a 404 page without affecting the browser history.
Using router.navigate(['/404']) or router.navigateByUrl('/404') doesn't seem to work because it adds both /user1/products/-1 and/404 to the browser history.
Meaning when I press the Back button in the browser, I go back to /user1/products/-1 which is immediately redirected to /404 and I'm essentially stuck at /404.
In ExpressJS, we would do something like next() to pass the request to our 404 handler. Is there a client-side equivalent in Angular 2?
Update
In the new Router V3 you can use guards as explained in https://angular.io/guide/router#canactivate-requiring-authentication
Original
I think you should use #CanActivate() to do the check. If you forward in #CanActivate() the invalid URL shouldn't be added to the history (not tried)
See also https://github.com/angular/angular/issues/4112 for how to use DI in #CanActivate()
Ok, this is already implemented, just not well-documented.
According to the docs:
router.navigateByUrl(url: string, _skipLocationChange?: boolean) : Promise<any>
Has a parameter _skipLocationChange that will not modify the history.
These will do the trick:
router.navigateByUrl('/404', true);
router.navigateByInstruction(router.generate(['/404']), true);
As of Angular 2 final this is the solution:
this._router.navigateByUrl('/404', { skipLocationChange: true })
Its really interesting question, perhaps you should report it as feature request. I would be nice to have access to router instruction inside loader callback of RouteDefinition.
You could try to emulate validation adding default route /** and using regex parameter of RouteDefinition to match only positive numbers.

Angular UI Router - allow ANY query string parameter

I am using the Angular UI Router and this works well in most situations. However, I have a situation where I don't know the names of the query string parameters ahead of time.
So normally with UI router you would define a route something like this:
$stateProvider.state('test', {
url: '/test?testQueryStringParam',
templateUrl: 'Test.html',
controller: 'TestController'
});
Then in my controller I can access the testQueryStringParam using $stateParams.
However, with UI router you can't access any query string parameters not specified in the route definition.
The router that comes with the Angular framework, does allow you to do this. So I have tried using the $location service with my UI router defintion. This does sort of work.
When I want to add a query string parameter I use:
$location.search("paramName", "paramValue");
When I want to get the query string values I just use:
$location.search()
This updates the URL, but doesn't re-instantiate the controller (like $state.go($state.current, {}, {reload: true}) would). This doesn't seem like a big problem because I can just re-load the data myself. However, if you use the back button in the browser, again it changes the URL, but doesn't re-instantiate the controller.
Is there anyway
I can get this to work using just the UI Router?
Get the workaround of using $location to actually re-instantiate the controller.?
As a last resort I also tried directing updating the window.location, but this refreshes the entire page which isn't acceptable.
Thanks
You can pass non url parameters that do not appear in URL
$stateProvider.state('test', {
url: '/test?testQueryStringParam',
params: {
optParam: null,
},
templateUrl: 'Test.html',
controller: 'TestController'
});
As you can see, optParam is an optional parameter with a default value of null and will not be visible in the URL
You can access this param in your controller using $stateParams
$stateParams.optParam
Here is a helpful blog

Laravel Route::get() function and parameter confusing

Hello everyone I just installed laravel4 and spend two days trying to make the first step. Now I made it but I'm confused about the Route::get() function and his paremeters.
I installe laravel directly in
/opt/lampp/htdocs/laravel
then follow tutorial to create file
userform.php
into app/views, then add following codes into routes.php
Route::get('userform', function()
{
return View::make('userform');
});
. Then I go to
/localhost/laravel/public
to see welcome page, and
/localhost/laravel/public/userform
to see the form defined in the view/userform.php.
Q1: According to chrome dev tools, i see in the html page, the form action is
http://localhost/laravel/public/userform
but there is nothing under public but
index.php, favicon.ico packages robots.txt
Q2: for
Route::get('userform', function()
{
return View::make('userform');
});
what is the first "userform" represent?? according the official tutorial, it's supposed to be url, but what is the former part?
for this line
return View::make('userform')
I guess "userform" referes to the file /app/views/userform.php, right?
The .htaccess file in the public directory is responsible for funnelling all incoming requests through the index.php file. This allows Laravel to grab the URI and match it to the route you defined and eventually return to you the view you made.
So you request localhost/laravel/public/userform, the request is funnelled through index.php and Laravel is booted. Laravel picks off the userform part of the URI and matches it against your defined routes. It finds the route you defined and fires it and returns the response.
You're spot on with what you were thinking with your second question as well. When you call View::make the first argument is the name of the view you want to "make". If you named your view app/views/forms/user.php then you would return it like so in your route:
return View::make('forms.user');
Or you could use a slash:
return View::make('forms/user');

Resources