I am using Codeigniter and want to have SEO-friendly URLs. There will be 2 types of URI segments, http://www.domain.com/view/193847 and http://www.domain.com/view/193847-canon-5d-mark-iii.
If the first URL is used, function view_mini is called and passed the product id 193847. If the 2nd one is used, function view_full will be called and passed the same product id 193847.
Problem: How can I differentiate between the 2 URLs? Or is this an inferior approach to solve the problem?
PHP How should the if condition be structured?
function view($pid) {
if($this->uri->segment(2) == something) {
$this->view_mini($pid);
} else {
$this->view_full($pid);
}
}
function view_mini($pid) {
// ...
}
function view_full($pid) {
// ...
}
EDIT
I am using URL routing to route http://www.domain.com/controllername/view/1234 to http://www.domain.com/view/1234
you can use the regular expression to check the segment if it has anything other than numbers, then you can execute the view that you want for example
$pattern = '\d[0-9][^a-zA-Z]';
$url = $this->uri->segment(2);
if(preg_match($pattern,$url))
{
//this will match only the numbers
$this->view_mini($pid);
} else {
$this->view_full($pid);
}
i hope this will help ..
Is there any definitive structure to the different URLs?
ie.
http://www.domain.com/view/[numbers]-[text]
If so, you could test the URL for that dash between the numbers and the dash?
Edit: un-tested
$route["view/(\d+)"] = "class/view_mini/$1";
$route["view/(\d+)-([\w'-]*)?/g"] = "class/view_full/$1/$2";
Use
$result = explode('-', $this->uri->segment(1));
If(isset($result[1]))
{
// show full view
} else {
// show mini view
}
$pid will always be $result[0]
Related
I currently use:
if(strpos($command->href,§current_view) !== false){
echo '<pre>true</pre>';
} else {
echo '<pre>false</pre>';
}
$command->href will output something like this: /path/index.php?option=com_component&view=orders Whereas
§current_view is outputting orders. These outputs are dynamically generated, but the scheme will always be the same.
What I need to do is return true/false if the words from $current_view match the view=orders in the URLs from $command->href. The issue with my code is, that it doesnt match anything.
What is the correct way to do this?
Please note that the $command->href and the whole code is inside a while function, that pass multiple URLs and this only needs to match the same ones.
Breaking it down to a simple example, using your code and variable values.
$current_view = 'orders';
$command = '/path/index.php?option=com_component&view=orders';
if(strpos($command,$current_view) !== false){
echo '<pre>true</pre>';
}
else {
echo '<pre>false</pre>';
}
The oputput is "true".
Now, go and debug the REAL values of $command->href and $current_view...
I'm pretty confident that the values are not what you think they are.
Does something like:
if(substr($command->href, strrpos($command->href, '&') + 6) == $current_view)
accomplish what you are after?
To explain, strpos get the last instance of a character in a string (& for you, since you said it always follows the scheme). Then we move over 6 characters to take "&view=" out of the string.
You should now be left with "orders" == "orders".
Or do you sometimes include some arguments after the view?
Try parsing url, extracting your view query string value and compare it to $current_view
$query= [];
parse_str(parse_url($command->href)['query'], $query);
if($current_view === $query["view"])
echo '<pre>true</pre>';
} else {
echo '<pre>false</pre>';
}
I'm creating an API with PHP and am having difficulty with the URL, it gets this URL to me http://localhost/api/index/Peoples/3
The idea is that I want to use the Peoples Class and I want to get the Person with code 3, is there any way I can get Peoples and number 3?
The idea I made was this, I retrieve the URL from the browser and I'm going to explode on it. While I do not need to pass parameters through the url this works perfectly
public function getClass(){
$params = explode("/api/index/", $this->currentUrl);
if(count($params) == 1)
return "no have class !";
else
{
$params = explode('/', $params[1]);
$this->callClass($params[0]);
return "Classe : ".$params[0];
}
}
with this code I can recover Peoples and then know which class I will use. now I want to pass the parameter of the code, as for example 3. I could pass as follows ../Peoples?id=3 but I would have to break even more my string, have a better way to do this?
What's the difference between passing ../Peoples?id=3 or ../Peoples/3 and how can I recover?
you can try something like this
public function getClass()
{
$path = explode('/', parse_url($this->currentUrl, PHP_URL_PATH));
if(count($path) !== 5){
return 'not valid url';
}
$id = array_pop($path);
$class = array_pop($path);
//$this->callClass($class);
$newPath = $class.'?id='.$id;
return $newPath;
}
and regarding your question about /Peoples?id=3 or ../Peoples/3
the second one is easier if you are working with a framework such as symfony or laravel where you can define controllers and the first one you can easily fetch you data from the URL with $_GET['id']
Hope that was what you are looking for.
I'm having php script that deals with thousands of queries starting just like (i.e. http://localhost:1234/browse.php?cat=2) so I don't want to write thousands of URLs in an array to deal with if and else condition such as below,
Please guide me how can i make it possible to use "?" sign in my url to distinguish between what command to process if url contains "?" sign.
I used "/browse.php?*" in code as shown in below example but it's not working for me still...Please guide because I'm new in php and search and lot regarding this answer but unable to find a single authentic answer for it, thanks
if(in_array($_SERVER['REQUEST_URI'],array('/browse.php','/browse.php?*')))
{
echo "<Something Like this 1>";
}
elseif ($url == "")
{
echo "<Something Like this 2>";
};
in_array would only check for a full match here and is not appropriate for what you are trying to do. PHP has many String Functions you should look at.
if (strpos($_SERVER['REQUEST_URI'], '?') !== false) {
//URL has '?' mark
}
else{
//URL has no '?' mark
}
I believe you are only concerned with the cat URL search parameter? If so, you can access this parameter in your browse.php script using the $_GET array:
<?php
if (array_key_exists('cat', $_GET)) {
echo "cat parameter: {$_GET['cat']}"; // display ?cat=value
} else {
echo 'No cat URL parameter'; // ?cat was not in the URL
}
?>
http://localhost:1234/browse.php -> No cat URL parameter
http://localhost:1234/browse.php?cat=57890 -> cat parameter: 57890
I have structure of URL like this: https://steamcommunity.com/tradeoffer/new/?partner=12a3456asdf789&token=12345Dasdew678
The two parameters of the URL, partner and token will have dynamic values which can contain alphanumeric characters only.
I would like to know if the format of the URL meets my requirements
if($URLisFormattedProperly) {
return true;
} else {
return false;
}
How should I do this?
Here you go, regex based on your existing URL
preg_match('/https\:\/\/steamcommunity\.com\/tradeoffer\/new\/\?partner=([a-zA-Z0-9]+)&token=([a-zA-Z0-9]+)/', $url, $matches);
if(count($matches)>0)
{
// this meets your criteria
}
I have been working on a fancy router/dispatcher class for weeks now trying to decide how I wanted it, I got it perfect IMO except performance is not what I am wanting from it. It uses a route map arrap = /forums/viewthread/:id/:page => 'forums/viewthread/(?\d+)' and loops through my map array with regex to get a match, I am trying to get something better on a high traffic site, here is a start...
$uri = "forum/viewforum/id-522/page-3";
$parts = explode("/", $uri);
$controller = $parts['0'];
$method = $parts['1'];
if($parts['2'] != ''){
$idNumber = $parts['2'];
}
if($parts['3'] != ''){
$pageNumber = $parts['3'];
}
Where I need help is sometime an id and a page will not be present sometime one or the other and sometimes both, so obvioulsy my above code would not cover that, it assumes array item 2 is always the id and 3 is always the page, could someone show me a practical way of matchting up the page and id to a variable only if they exist in the URI and without using regular expressions?
You can see what I have so far on my regular expressions versions in this question Is this a good way to match URI to class/method in PHP for MVC
This seems more extendable:
$parts = explode("/", $uri);
$parts_count=count($parts);
//set default values
$page_info=array('id'=>0,'page'=>0);
for($i=2;$i<$parts_count;$i++) {
if(strpos($parts[$i],'-')!==FALSE) {
list($info_type,$info_val)=explode('-',$parts[$i],2);
if(isset($page_info[$info_type])) {
$page_info[$info_type]=(int)$info_val;
}
}
}
then just use $page_info values. You can easily add other values this way and more levels of '/'.
if ( ! empty($parts['2']))
{
if (strpos($parts['2'], 'id-') !== FALSE)
{
$idNumber = str_replace('id-', '', $parts['2']);
}
elseif (strpos($parts['2'], 'page-') !== FALSE)
{
$pageNumber = str_replace('id-', '', $parts['2']);
}
}
And do the same for $part[3]