I have Backbone js application and i have setup the router and now i try to pass url parameter as query string.
routes: {
'login?r=:query': 'doSomthing'
}
Can someone help me to pass url(http://someurl.org/dsfgsdjfsdgf) us query parameter.
For this question i found the right answer
router: {
'login?r:*query': 'dosomthing'
}
Related
I have a laravel application which uses angular js as the front end. There I need to disable this route. appo.dev/ which means the root path of the application. How can I disable that path only. Here I need to access other routes such as appo.dev/progess. I tried the following way in routes.php file. It's better if I can find a solution with this code.
Route::any('{path?}', function () {
return view("appo_app");
})->where("path", ".+")
->whereNotIn("path", "appo.dev/");
Above without whereNotIn clause it will work for all the routes. So I am thinking a way to disable only that particular route via wherenotin clause. Or is there a better wild card character? Anyone knows how to solve this issue.
Maybe without what you want is like the root route did not exist, you can try this.
And it will return an error 404. Maybe that's what you're looking for?
I assume that appo.dev is the domain.
Route::any('/', function () {
abort(404);
});
Route::any('{path?}', function () {
return view("appo_app");
})->where("path", ".+");
If it were not the domain, maybe this will help you.
Route::any('appo.dev', function () {
abort(404);
});
Route::any('appo.dev/{path?}', function () {
return view("appo_app");
})->where("path", ".+");
I hope it helps you. A cordial greeting.
I am new to react router. I use react-router-dom 4.2.2
in my router set up I have:
<Route path={"/confirmuser/:confirmation_code/:username"} component={ConfirmUser} />
and here is a sample url I am trying to achieve:
localhost:3003/confirmuser?confirmation_code=986797&username=5f1
As you see I am trying to send multiple query strings.
in the confirmUser I read the query strings as follow:
console.log(this.props.match.params.confirmation_code);
console.log(this.props.match.params.username);
However I do not even get directed to this component and it seems react is not able to route to that page properly.
Any idea?
React-router v4 doesn't parse query strings anymore, so you either have to do the parsing yourself (not recommended), or use a package like query-string. An easy way to access the values is with a wrapper component, like this:
import * as queryString from 'query-string';
..
const WrappedConfirmUser = () => {
const {confirmation_code, username} = queryString.parse(location.search);
return <ConfirmUser confirmation_code={confirmation_code} username={username}/>;
}
You are trying to map search-params to path segments?
The Route you defined will try to match the path, not the search params.
Try:
http://localhost:3003/confirmuser/986797/5f1
and the values will be in this.props.match.params like this:
{
confirmation_code: '986797',
username: '5f1',
}
if you still want to read the search params, you can access them from this.props.location.search, but react-router will not match them to a route for you.
Your path doesn't match your url.
It matches localhost:3003/confirmuser/986797/5f1
Then you can access params using extra prop match:
{props.match.params.confirmation_code}
{props.match.params.username}
I hava a list page:
$routeProvider.when('/', {
redirectTo:'/1', // so default will be page 1
}).when('/:page',{
controller:'listCtrl',
templateUrl:'xxxxx'
})
In the controller will use $route.current.params.page to do the logic.
The question is:redirectTo will cause a browser 301 which I don't want.(bad performance) And I also don't want everytime one click the main nav button, it would cause a 301.
How can I archive the same goal without use redirectTo?
It's something like: if I use the route same as '/:page'(write the same controller / templateUrl), how can I pass the page number(1) to the controller without route param?
EDIT:
Thanks to the accepted answer.
Changed the title to Flask related.
Found that the 301 was caused by Flask's defaults route:
#bp.route('/list', defaults={'page':1})
#bp.route('/list/<int:page>')
def lst(page):
when the request uri is /list/1, will be 301 to /list... don't know how to resolve yet, but this is another question.
redirectTo in $route doesn't actually do an HTTP redirect - it just redirects logically to the right ng-view. So, there isn't any performance hit.
But just for completeness-sake, you can pass a parameter in the resolve property:
.when('/:page?',{
controller:'listCtrl',
templateUrl:'xxxxx',
resolve: {
page: function($route) { return $route.current.params.page || 1; }
}
})
I have multiple routers in my app, in general way it looks like this:
// Start backbone.js
if (!Backbone.History.started) {
Backbone.history.start({pushState: true, hashChange: false});
}
// Perform some RPC requests ...
// Depending on user role, received from the server should be created suitable router:
var router;
if (typeof app.user.role === 'manager') {
router = new routers.manager();
} else {
router = new routers.guest();
}
Problem is that after page is loaded and script is executed routers do not do. anything. They do not load route for current url automatically. So, i had to fix it this way (i am not sure that it is a right way):
routers.guest.initialize = routers.manager.initialize = function() {
var defaultRoute = 'default';
if (typeof this.routes[Backbone.history.fragment] !== 'undefined') {
this[this.routes[Backbone.history.fragment]]();
} else {
this.navigate(defaultRoute, true);
}
};
It is working fine, except one bug: when i use route with params, for example /reset-password-confirm/:code - it is unable to find in in routes property. I could write some more code to fix it, but i suppose that i am doing something wrong, if i have to write such things - as i understand router should handle routes just after it was created.
So, questions:
Why my router(s) does not handle routes for current url after it is being created? Perhaps i need to start backbone history later? (but this bug will happen again later then)
How it is possible to make routes with params like /user/:id work there?
Perhaps it is bad idea to re-create routers? Perhaps it is better to create all of them one time?
P.S. I've tried to create both routers and keep them, also i've trie to call backbone history start method after all routers were created.. but this didn't help :/
Assuming you route is declared as the following:
routes : {
'/user/:id' : 'user'
}
Your initialize code is not working because when you initialize your router with a url such as: /user/1234. Backbone.history.fragment will be /user/1234 (not /user/:id). Since the this.routes object doesn't have a key of /user/1234, your else clause calls the default route.
If you first instantiate your router then call Backbone.history.start(), you will be able to remove your router initialize code. When you navigate to a url as /user/1234 your router will match the /user/:id route and call the user function.
The following should work for you without adding your initialize code:
var router = (app.user.role === 'manager') ? new routers.manager()
: new routers.guest();
Backbone.history.start({pushState: true, hashChange: false});
Looking at the code, seems like you're starting the backbone history before initializing any routes. That's most likely not goning to work.
The correct way of doing this type of seperation is by creating all the routes based on the role received from the server and then start the backbone history. Here's an SO thread that talks about it with code samples as well : How to protect routes for different user groups
Anybody know, how to get url array params outside controller (for example bootstrap.php)?
CakePHP is still PHP at the end of the day, so you can use any valid PHP method to achieve this. Like:
if (!empty($_SERVER['QUERY_STRING'])) {
// A querystring was set...
}
Or
$url = parse_url($_SERVER['REQUEST_URI']);
if (!empty($url['query'])) {
// A querystring was set...
}
After that you can do anything else that you want to do.