How to use $routeProvider with $_GET Parameters - php

I'm new with the angularjs and I want your help. I'm trying to include the $routeProvider into my project in order to use templating system.
$routeProvider.when('/admin.php?page=all_transactions', {
templateUrl : 'pages/home.html',
controller : 'TransactionsController'
});
I saw that most of the examples i found the url of .when had the following format /route1/:param for urls like #/route1/12345
Because I'm using the angular in wordpress admin page I want the .when to work with $_GET parameters like the one I gave with the example code.
The depth of parameters I want it to be up to 3 and ignore any other parameters.
Does anyone know how I can do it?

is it enough for u to know, that the params are there? Or do u need explicit values. If the params are enough u could follow this example: URL Routing with Query Parameters
url: "/contacts?myParam"
// will match to url of "/contacts?myParam=value"
If you need to have more than one, separate them with an '&':
url: "/contacts?myParam1&myParam2"
// will match to url of "/contacts?myParam1=value1&myParam2=wowcool"
Hope this helps.
Edit: For accessing the values u can do the following: Accessing query parameter values
Also can get other arbitrary params in the query string form /view/1/2?other=12 with $routeParams.other – DavidC Aug 17 '14 at 21:04
OR:
While routing is indeed a good solution for application-level URL parsing, you may want to use the more low-level $location service, as injected in your own service or controller:
var paramValue = $location.search().myParam;
This simple syntax will work for http://example.com/path?myParam=someValue. However, only if you configured the $locationProvider in the html5 mode before:
$locationProvider.html5Mode(true);
Otherwise have a look at the http://example.com/#!/path?myParam=someValue "Hashbang" syntax which is a bit more complicated, but have the benefit of working on old browsers (non-html5 compatible) as well.

Related

Using multiple nested _GET variables in a single URL

Setup:
Script that generates word images from multiple letter images
(autotext.php)
URL is formatted:
www.whatever.com/autotext.php?text=hello%20world
Script that alters images server-side to run filters or generate
smaller sizes (thumbnail.php)
URL is formatted:
www.whatever.com/thumbnail.php?src=whatever.png&h=XXX&w=XXX
Use-case:
I want to generate a smaller version of the autotext server-side. So my call would look something like:
www.whatever.com/thumbnail.php?src=autotext.php?text=hello%20world&h=XXX&w=XXX
As you can see, I would like to treat a URL with _GET variables as a variable itself. No amount of playing with URI encoding has helped make this work.
I have access to the PHP for both scripts, and can make some simple alterations if that's the only solution. Any help or advice would be appreciated. I would not even rule out a Javascript frontend solution, though my preference is to utilize the two scripts I already have implemented.
You should be able to do this by urlencoding all the $_GET params into a variable then assigning that variable to another, like this (untested):
// Url generation
$url = www.whatever.com/thumbnail.php?src=(urlencode(http_build_query($_GET)));
Then you should be able to retrieve on other side:
$src = urldecode(explode('&', $_GET['src']));
I've seen this exact behavior when trapping where to redirect a user, after an action occurs.
---- Update ----
Your "use case" url was correct:
www.whatever.com/thumbnail.php?src=autotext.php?text=hello%20world&h=XXX&w=XXX
.... except that you CANNOT have more than one ? within a "valid" url. So if you convert the 2nd ? to a &, you should then be able to access $_GET['text'] from the autotext.php script, then you can urldecode it to get the contents.

PHP: Sending a request and getting a response

I'm from ASP.NET MVC background and this is first time I'm trying to write something in PHP.
In ASP.NET MVC we can develop models for our data and using the actions that we write we can get them or send them to another action. What I mean is that
public ActionResult Login_Action(LoginModel _Model) {
// Authenticating the user
return RedirectToAction(X);
}
when calling this the url that is shown in the address bar (in case of using GET, if it is POST nothing will be shown after the page name) will be:
www.WebsiteX.com/Login?Username=something&Password=something
The problem is that I don't even know how search for this in google (like by typing what exactly) because in Microsoft side, these are handled automatically the way I described.
But in case of PHP, how can I get the values in the address bar? do I have to get the actual address and then break the values down into arrays?
I'd appreciate any help.
First of all, this seems to be invalid for me: www.WebsiteX.com/Login?Username=something?Password=something The first parameter need to be ? and the others should be &.
Second: You can get your values of your parameters by accessing the $_GET global array.
Eg. for the username echo $_GET["Username"];
Are you using any framework? You should. And then, the Framework will give you the way to do that. In ASP.NET you use a Framework so do the same in PHP.
With vanille PHP you can get the GET values with $_GET['Username']. But please, use a framework.
I think that the most popular are Laravel and Symfony right now.
Example:
In laravel you can bind a parameter to a variable so you can do something like:
//Url: mywebsite.com/user/1/
Route::get('user/{id}', function($id)
{
return 'User '.$id;
});
Which is similar with the ASP.NET example.

How to rewrite URL structure for JavaScript

I have a web page with basicly the following URL structure:
www.example.com/main.php?userId=mattias.wolff
www.example.com/definitions.php?userId=mattias.wolff
www.example.com/tasks.php?userId=mattias.wolff
www.example.com/profile.php?userId=mattias.wolff
What I would like to do is to change this to get rid of the parameters:
www.example.com/mattias.wolff
www.example.com/mattias.wolff/definitions
www.example.com/mattias.wolff/tasks
www.example.com/mattias.wolff/profile
Server side this is not a problem since I can just use mod rewrite to rewrite the URLs to the "old" format (including paramters etc.)
My question is how this should be handled client side? The pages content is very much generated by JavaScript and I therefore need to get the parameters in the same way as before.
Is there some best practice that I have missed here? Writing a function on that parse the new URL in Javascript or send the "old" URL from server side in some kind of parameter?
Do not forget that essentially, the URL is a (kind of a) query, too. The main difference here is whether you are using named parameters or positional parameters.
The ? notation is essentially a standard to allow browser to construct an URL from a form query automatically.
You could as well be using URLs of the scheme:
www.example.com/name=mattias.wolff/page=definitions
if that is what you want.
My recommendation for you is to really define a URL scheme for your pages that completely suits your needs and has enough room for future extension. Ideally, you should be able to switch back to the old scheme at some point if necessary, without major name conflicts.
There is clearly nothing wrong with organizing your URLs in the scheme of /[username]/[page], and using this scheme from JavaScript (both for parsing and generating links!) as long as you don't change it all the time.
With the following simple(?) function you can transform a URL in the way you indicate:
function transform (href) {
var m = href.match (/((?:[^\/]+\/\/)?[^\/]+\/)(.*)\.php\?userId=(.*)/);
return m ? m[1] + m[3] + '/' + m[2] : href;
}
Basically the function extracts the components of the URL into an array using a regexp match then reassembles the URL in the way you want. If the match fails the original URL is returned.
Tested on Chrome with :
var test = ["www.example.com/main.php?userId=mattias.wolff",
"www.example.com/definitions.php?userId=mattias.wolff",
"http://www.example.com/tasks.php?userId=mattias.wolff",
"www.example.com/profile.php?userId=mattias.wolff"];
for (var i = test.length; i--;)
console.log ('"' + test[i] + '" --> "' + transform (test[i]) + '"');
Output :
"www.example.com/profile.php?userId=mattias.wolff" --> "www.example.com/mattias.wolff/profile"
"http://www.example.com/tasks.php?userId=mattias.wolff" --> "http://www.example.com/mattias.wolff/tasks"
"www.example.com/definitions.php?userId=mattias.wolff" --> "www.example.com/mattias.wolff/definitions"
"www.example.com/main.php?userId=mattias.wolff" --> "www.example.com/mattias.wolff/main"

how to process php REST url resources

I have read many about REST api in php articles. but I still get quite confusing.
they basically rewrite the url to a index.php, which process the url and depends on the method, then send response
but which is the properly way to process the url? this looks doen't look correct...
get the uri and split it
I should know what to do with each portion, eg. for GET /usr/1 I should do something like:
if($myUri[0]=="usr")
getUser($myUri[1]);
if the request url is like GET www.domain.com/user/1
it would call getUser($id);
but what happen if you can also retrieve the user by name, or maybe e-mail? so the url can also be www.domain.com/user/john or www.domain.com/user/john#gmail.com
and each url should call different methods like getUsrByName($name) or getUsrByEmail($mail)
The proper way of handling this would be to have URLs like this:
domain.com/user/id/1 -> user::getById
domain.com/user/email/foo#bar.com -> user::getByEmail
domain.com/user/username/foo -> user::getByUsername
However, specifying multiple "parameters" is more like a search, I'd go against using resources for that, because a path should be absolute. Which means:
domain.com/user/name/Kossel/likes/StackOverflow
And:
domain.com/user/likes/StackOverflow/name/Kossel
Are not the same resource. Instead I'd do:
domain.com/user/?name=Kossel&likes=StackOverflow
This is what Stack Overflow uses:
stackoverflow.com/questions/tagged/php
stackoverflow.com/tags/php/new
stackoverflow.com/questions/tagged/mysql?sort=featured
To avoid long if/else statement, use variable function names. this allows you to use the url string to call the correct function.
http://php.net/manual/en/functions.variable-functions.php
Also, you may want to use classes/class methods instead of functions. this way you can set up an __autoload function, which will allow you to only load code that you are going to use each time the index.php is called.
MVC architecture usually breaks their urls into /class_name/class_method_name/arguments...

variable number of parameters from a url/paramname/valueparam in an array?

basicly I would like to read url params in an array so finding params don't depend on their place in url
I have a url for seach with controller/action/paramA/valueparamA/paramB/valueparamB
theses params are optional : I have direct url with search params inside
to read params from url we have to use action(valueparamA, valueparamB)
but for me it seems really rigid
I want to read parameters by their name, not by their place in url!
so I can have different urls like
urlA = controller/action?paramA=valueA
*(or controller/action/paramA/valueA)*
urlB controller/action?paramB=valueB
than I can use with the same action, like we do with a form with $_POST array (it and $_GET[} seems always empty when direct url params)
the best would be to have all parameters in an array[paramname=>paramvalue] like in a form
what I DON'T want is tu use differents actions for different parameters possibles! :)
the best I saw was to use juste on array like parameter :
controller/action/array[paramname=>paramvalue]
(passing arrays as url parameter)
but it seems to complicate something basic :
just read the normal url parameters like every framework knows :) with
url?nameparam=valueparam&...
hope there is a solution !
I begin tor eally like the light and quick of ci but sometimes (like for extending model) it seems a little "rigid" ;)
think in advance for any idea!
Yep the URI class provides this functionality in the form of
$this->uri->uri_to_assoc(n);
This returns an array containing all parameters (keys and values must be defined in the URL).
Full details can be found on the Codigniter Userguide:
http://codeigniter.com/user_guide/libraries/uri.html

Categories