i'm very new to Yii, Normally yii serve like this url format
baseurl/app_dir/conroller/action/parametername/vaule
But i want to show up it like this
baseurl/app_dir/conroller/action/vaule
means without parameter name.
i read lot of solutions but couldn't find, does anybody knows how to do it, please help me.
It is somewhere in docs, here is example:
'/user/<id:\w+>/' => 'user/view/'
This means, that request /user/someUserName will be directed to action view in controller user with parameter id with value someUserName. This way you have urls with just value, but in application it is available as named $_GET parameter.
NOTE: \w+ is just regex, which are supported here, you could for example use \d+ to limit value to numeric etc.
Related
I want a way to know the available or allowed arguments in url
for example:
http://www.example.com/?name=aaa&id=123
but there might be another parameters but it is not visible like ( age=555&email=aa#bbb.com)
so most of urls will work even if I pass 1 parameter especially for search engine
but how can I know the available arguments that I can use?
thank you
The arguments that a particular URL can accept are determined by the site's author. If they don't publish that information the only way you can know a particular argument is acceptable is to submit it and see whether you get an error response. Even then you won't know what range of values are acceptable.
I want to know how this code is working:
memberpage.php?action=admin_mail_list&type=outbox
Yes, memberpage.php is a page but is admin_mail_list&type=outbox a separate page?
If No, what is it then?
If Yes, why is there no file type after the name (I mean .php or .html)?
That link is using the GET method, meaning variables are defined in the URL rather than the PHP code itself.
For example, if you were to run a Google or Bing search, it wouldn't just be:
https://google.com/search
It would be something like:
https://www.google.co.uk/search?q=test
The benefit of using this, is if the page is refreshed or sent to a friend, the variable won't need to be redefined like POST, it's already defined in the URL.
So, for example, you may have :
http://example.com/example?q=test
The /example page would have this PHP code:
echo $_GET['q'];
which would print "test".
See the following pages if you need more help.
http://php.net/manual/en/reserved.variables.get.php
https://www.tutorialspoint.com/php/php_get_post.htm
You're describing two different parts of a URI. This isn't exclusive to PHP, the URI recommendation applies to all websites regardless of their programming language.
The first (memberpage.php) is the path, and W3 describes it like this:
Path
The rest of the URI follows the colon in a format depending on the
scheme. The path is interpreted in a manner dependent on the protocol
being used. However, when it contains slashes, these must imply a
hierarchical structure.
and the second (?admin_mail_list&type=outbox) is the query string and is described like this:
Query strings
The question mark ("?", ASCII 3F hex) is used to delimit the boundary
between the URI of a queryable object, and a set of words used to
express a query on that object. When this form is used, the combined
URI stands for the object which results from the query being applied
to the original object.
Within the query string, the plus sign is
reserved as shorthand notation for a space. Therefore, real plus signs
must be encoded. This method was used to make query URIs easier to
pass in systems which did not allow spaces.
The query string represents some operation applied to the object, but
this specification gives no common syntax or semantics for it. In
practice the syntax and sematics may depend on the scheme and may even
on the base URI.
To put it simply, the path of the URI dictates which script is to be run, and fields in the query string are parameters to use in that script.
If you're familiar with working on the command line it might be easier to think of these parameters like options on a command line utility. A comparable command might look something like this:
$ php memberpage.php --admin_mail_list --type=outbox
It's important to remember that parameters like this aren't necessarily required to access the URI so it's inappropriate to think of these are arguments on a command line. If your script absolutely needs these parameters to function, you must create that logic within the script yourself, as it is not enforced by the URI.
To answer your question directly:
Yes!
Passing different parameters to the URI can lead to wildly different pages.You absolutely should consider different URI's to be different pages because from the perspective of your users and the larger web, they certainly are. Both users and search engines will consider them distinct and so should you.
That means that the "memberpage.php" script takes two parameters via $_GET:
"action" which has the value "admin_mail_list"
"type" which has the value "outbox"
See: http://php.net/manual/en/reserved.variables.get.php
This isn't the best question ever, but since search engines feel the need to ignore symbols, I have to ask somewhere.
In a link, I'll sometimes see a ?, such as [link]/file.extension?some_type_of_info, or even +,&,=, etc ('best example' of what I mean is youtube videos). What are these called and what do they do? A good site would be great to :)
I am mostly interested because I have a site that loads stuff into a page, and currently the way I allow 'bookmarking' a page (or more important to me, being able to go back a 'page') is use hash values to represent my 'page'.
Ultimately I would like to not have the page refresh, which is why hash values are good, but I'd like alternatives if any (not really what hashmarks are meant for, but mostly different browsers seem to treat assigning the hash values in jquery differently)
Again, sorry this is mostly just a "what is this" question, but if anyone could tell me pros/cons towards using the method in question versus hash values, that would be great also :)
See the url specification, in particular the section syntax components:
URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
… and the definition of query.
The query component contains non-hierarchical data that, along with
data in the path component (Section 3.3), serves to identify a
resource within the scope of the URI's scheme and naming authority
(if any). The query component is indicated by the first question
mark ("?") character and terminated by a number sign ("#") character
or by the end of the URI.
Ultimately I would like to not have the page refresh
Use the history API. This is independent of the structure of the URL (other than having to be a URL on the same origin).
The part after the ? is called query string. It's used to pass parameters to a web site. Parameters are separated using the & sign. For example, this would pass parameters to a site:
http://test.site.tld/index.php?parameter=value&another=anotherValue
This would pass the parameters "parameter" (with value "value") and the parameter "another" (with value "anotherValue") to the script index.php.
The + sign is sometimes used to represent a space. For example, "Hello World" could be represented as "Hello+World" or "Hello%20World".
A # sign is used to jump directly to an anchor within the page. For example
http://test.site.tld/index.php#docs
Would jump to the anchor "docs" within the web site.
The ? in a URL introduces the query string, which is data provided to the server. Everything prior to the ? specifies the resource on the server (in theory), and everything after it is additional data.
So for example:
http://example.com/foo/bar/page.php?data=one
http://example.com/foo/bar/page.php?data=two
Both URLs cause the page.php page to be retrieved by the server, and since it's a PHP page, a properly-configured server will run the PHP code within it. That PHP code can access the query string data as one big string via $_SERVER['QUERY_STRING'], or as a series of name/value pairs (if that's what it is, it doesn't have to be) via $_GET['paramname']. Note that that's _GET because query string parameters are GET parameters; POST parameters are sent via a different mechanism (not available for just links; you need a form or similar).
The stuff at the end of the url is a querystring. ? is used to denote the beginning of a querystring, they use key=value pairs seperated by &.
To address your question of whether this can be used for bookmarking, I believe the approach you are currently using with URL hashes (#) is correct.
The ? part is a querystring, GET parameters are sent that way.
The more interesting part of your question is: how can I enable the back-button/history for users in a dynamic website? Check out this library: https://github.com/browserstate/History.js/
It enables you (for newer browsers) to get/set history states. Each dynamic page gets it's own address. For older browsers, there is the hash-bang fallback (#/page/page).
I'm using Codeigniter(PHP Framework) and I have URLs like http://www.mysite.com/age/21/gender/female/name/jamie/city/boston/userid/1234.
Is it possible to make the URL more SEO/user friendly? Like http://www.mysite.com/1234-Jamie-Boston and somehow still be able to pass the values found in the original URL strings like age =>21 and gender => female?
I think you could - use mod_rewrite to rewrite your new url from http://www.mysite.com/1234-Jamie-Boston to http://www.mysite.com/user/1234. In your user controller load from the db the one with id 1234 and set all his other properties
Or without the mod_rewrite - you could modify your application/config/routes.php file with an entry like:
$route['(\d+)-.*'] = "users/display_user/$1";
This way your SEO friendly urls will point to class Users, method display_users and you will get the id as parameter
The way that PHP works is that if you send 15 parameters to a function which only requires two, then PHP can more or less "swallow" the other arguments (they're still accessible, but they are less than optional). In your case, I would create a Users controller, perhaps with a display function which only takes one parameter, the user ID. That way, you can have /users/display/1234/whatever/you/would/like/zodiac_sign/stop_sign/favorite_letter/favorite%20punctuation/... Need I go on?
You can get around the need for a controller/method as part of your URL by using the $routes config file... In this case, I don't think user/display would really hurt SEO. I would not use mod_rewrite to do that simply because the architecture is already there in CodeIgniter.
So, here's an example on Forrst, a CodeIgniter website:
http://forrst.com/posts/PHP_Nano_Framework_WIP_Just_throwing_some_ideas_o-mU8
Look at that nice URL. You've got the root site, then posts, then the post title and a short extract. This is pretty cool for user experience.
However, my CodeIgniter site's URLs just plain suck. E.G.
http://mysite.com/code/view/120
So it accesses the controller code, then the function view, then the 20 on the end is the Post ID (and it does the database queries based on that).
I realised I could do some routing. So in my routes.php file, I put the following in:
$route['posts/(:num)'] = "code/view/$1"; - so this will make http://mysite.com/posts/120 be the same as http://mysite.com/code/view/120. A bit nicer, I think you'll agree.
My question is - how can I use a similar technique to Forrst, whereby an extract of the post is actually appended to the URL? I can't really see how this would be possible. How can the PHP script determine what it should look up in the database, especially if there are several things with the same title?
Thanks!
Jack
To get a URL like in your example you need to add a routing rule, like you've already done $route['posts/(:num)'] = "code/view/$1";. Forrst's url seems to be "mapped" (or something like that), I think the last part of the uri is the identifier (o-mU8 seems like a hash, but I prefer an int id) which is stored in the db, so if he queries, he splits the uri by the ndashes (_), and gets the last part of it, like this within your controller action:
$elements = explode('_',$this-uri-segment(2));
$identifier = $elements[count($elements)-1];
$results = $this->myModel->myQuery($identifier);
Basically the string between the controller/ and the identifier is totally useless, but not if your goal is a better SEO.
I hope this helps
See the official dicussions. The term that is often related to this is "slug". Haven't tested the approach from the CI forums myself, but the suggestions and examples look pretty good.
The URL helper in codeigniter has a function call url_title(). I haven't used it myself but I think it's what you are looking for.