How do I get the suffix (in code) that is being used for urls? - magento

Magento can add a suffix that is defined by the user to append onto urls. I want to get that suffix from my code. Does anyone know an easy way to do this?

If it's stored in the configuration area, then you access it just as you would any other configuration value, by using Mage::getStoreConfig($config_path) where $config_path is defined in the system.xml of the module that defines it.
If you're not sure of the $config_path, then I usually cheat and inspect the textbox/dropdown in the configuration section, take a look at the id, e.g. dev_log_file, and translate it to dev/log/file. You'll need to use some intelligence when there are multiple _ though :)

Nick's answer is good but the actual answer to this question is:
$suffix = Mage::helper('catalog/category')->getCategoryUrlSuffix();

If I am not mistaken, here is the code ( because I don't understand what you want with URL )
<?php
$currentUrl = $this->helper('core/url')->getCurrentUrl();
$url_parts = split('[/.-]', $currentUrl); // escape characters should change based your url
echo $url_parts[0]; //check here
?>

complete product url:
$productId = ***;
$productUrl = Mage::getBaseUrl().Mage::getResourceSingleton('catalog/product')->getAttributeRawValue($productId, 'url_key', Mage::app()->getStore()).Mage::helper('catalog/product')->getProductUrlSuffix();

Related

Laravel 8 multilanguage routes and how get the translated link of the same page

I'm trying to make my test app in multilanguage way.
This question has two correlated questions:
First question:
I followed the second answer in How to create multilingual translated routes in Laravel and this help me having a multilanguage site and the route cached, but I've a question and some misunderstanding.
It's a good practice overwrite an app config as they do int the AppServiceProver.php, making:
Config::set('app.locale_prefix', Request::segment(1));
Isn't better to work with the Session::locale in any case?
Second question:
In my case I've two languages, and in the navbar I want to print just ENG when locale is original language, and ITA when session locale is English.
If I'm in the Italian page, the ENG link in the navbar should point to the same English translated page.
Working with the method used in the other question, I hade many problems caused by the:
Config::set('app.locale_prefix', Request::segment(1));
We overwrite the variable in the config file local_prefix, and every time I switch to English language the locale_prefix will change to 'eng' and this sounds me strange, another thing I did is this:
if ( $lang && in_array($lang, config('app.alt_langs')) ){
return app('url')->route($lang . '_' . $name, $parameters, $absolute);
}
We use the alt_langs where are defined only the alternative languages, and this is a problem cause if I pass the local lang, in my case 'it', like lang parameter, this will not be found cause, from the description, the alt_lang should not contain the locale language and you will be able to get only the translated string.
If I change the:
if ( $lang && in_array($lang, config('app.alt_langs')) ){
return app('url')->route($lang . '_' . $name, $parameters, $absolute);
}
in:
if ( $lang && in_array($lang, config('app.all_langs')) ){
return app('url')->route($lang . '_' . $name, $parameters, $absolute);
}
Now using app.all_langs I'm able to choose which URL you want and in which language I want.
How do I get the translated URL?
In the blade file I need to get the translated URL of the page, and if read the other question, we used the $prefix for caching the routes and giving to the route a new name ->name($prefix.'_home'); in this way I can cache all the route and I can call the routes using blade without prefix {{ route('name') }} but, needing the translated url of the actual page a made this on the top of the view:
#php
$ThisRoute = Route::currentRouteName();
$result = substr($ThisRoute, 0, 2);
if ($result =='it' ){
$routeName = str_replace('it_', '', $ThisRoute);
$url = route($routeName,[],true,'en');
} else {
$routeName = str_replace('en_', '', $ThisRoute);
$url = route($routeName,[],true,'it');
}
#endphp
Doing this I get the actual route name that should be it_home I check if start with it_ or en_, I remove the it_ or en_ prefix and I get the translated URL, now you can use the $url as <a href="{{ $url" }}>text</a> cause if I call the {{ route('page') }} I get the link, with the locale language.
This code is not very good, I know, but I written in 5 minutes, need more implementation, and check, but for the moment is just to play with Laravel.
It's a good way?? How can I do it better (except the blade link retrieving)?? Many solution I found used middleware, but I would like to avoid a link in the navbar like mysite.com/changelang?lang=en
Is a good approach overriding the app.locale_prefix?
First
according to your question, it's a bad practice to save the preferences into .env or session because as soon as the session is finished the saved language will be removed also it's common when you need to store any preferences related to your website such as (Color, Font, Language, ...etc) you must store any of them into the cache.
Second
honestly, your code is a very strange and NOT common way and there are two ways to handle what do you need
First
There is a very helpful and awesome package called mcamara it'll help you too much (I recommend this solution).
Second
you can do it from scratch using the lang folder located in the resource folder and you must create files with the same count of the needed languages then use the keys that you'll define into these files into views and you can prefix your routes with the selected language you can use group method like so
Route::group(['prefix' => 'selected_lang'], function() {
Route::get('first_route', [Controller::class, 'your_method']);
});
or you can add the selected language as a query string like so localhost:8000/your_route?lang=en you can follow this tutorial for more info.

Get segment from url CodeIgniter but not first

I know i can get all segments from url like this
Lets say i have this example link
www.example.com/de/products.html
Using url_helper like this:
$data['url'] = $this->uri->uri_string();
I will get value like this
de/products
But i dont need first segment de, only products, the problem is that
i dont know how many segments it will be, i only need to remove the first
Is there possible to forget first segment with url helper in CI?
Try like this...
Use the php's explode() function to make the url string as array.Then apply array's array_shift() function which always removes the first element from array.
Code is looks like as below
$data= $this->uri->uri_string();
$arr=explode('/', $data);
array_shift($arr);
//print_r($arr);
Then use the php's implode() method to get the URI without first segment.Hope it will works...
$uri=implode('/',$arr);
echo $uri;
There is no URL helper in the CI to forget the first segment. However you can easily make a custom one and put #Hikmat's answer below it in the application/helpers/MY_url_helper.php in the Core folder.
e.g.
function my_forget_first_segment() {
$data= $this->uri->uri_string();
$arr=explode('/', $data);
array_shift($arr);
$uri=implode('/',$arr);
return $uri;
}
Before Edit answer.
You need to try this
$second_segment = $this->uri->segment(2);
From Codeigniter documentation -
$this->uri->segment(n);
Permits you to retrieve a specific segment. Where n is the segment number you wish to retrieve. Segments are numbered from left to right. For example, if your full URL is this:
http://example.com/index.php/news/local/metro/crime_is_up
The segment numbers would be this:
1. news
2. local
3. metro
4. crime_is_up
The optional second parameter defaults to NULL and allows you to set the return value of this method when the requested URI segment is missing. For example, this would tell the method to return the number zero in the event of failure:
$product_id = $this->uri->segment(3, 0);
example:
<?php
$data=$this->uri->segment(2);
$val=explode('.', $data);
echo $val[0];
?>

How to get the full URL of the route?

I have a Sinatra template file, which sends a POST request to the route /en/signup (en is the locale).
I need to extract the en from /en/signup. I tried to use request.path in the following code, but contains only /signup, not /en/signup. The log file shows that /en/signup was called.
What construct can I use in the route post '/signup' in order to get /en/signup?
Wake up, Neo.
From route file:
before '/:locale/*' do
I18n.locale = params[:locale]
request.path_info = '/' + params[:splat ][0]
end
That solved my problem: redirect to('/' + I18n.locale.to_s + '/signup-success').
If you can use java:
var url = document.URL;
var pieces = url.split("/");
Then simply split where and when you need to. The variable pieces is an array. For further splitting use pieces = pieces[1 (or others) ].split("/ (or others ");
I hope this helps!
Hello Sir if you use php try the code below. it will get the full path of the url for instance /example/en/signup.
$_SERVER['REQUEST_URI']
In Addition you can get including the http host (localhost) try the code below
$output = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $output;

Append variables to URL after pagination's '/page/x'

I'm using a couple URI variables to handle sorting a table, like this
.../page/7/sortby/serial_number/orderby/desc
as you can see, I'm also using the built in CI pagination library. My problem right now is that the links created with $this->pagination->create_links(); strip off sorting variables from the URI, making it difficult to maintain these sorting options between pages.
How can I go about appending these variables sortby/foo/orderby/bar to the URI of links created by the pagination library?
You can use the base_url option, and the page number segments will have to be last. It's a little annoying, but I think it's the simplest way.
// Get the current url segments
$segments = $this->uri->uri_to_assoc();
// Unset the "page" segment so it's not there twice
$segments['page'] = null;
// Put the uri back together
$uri = $this->uri->assoc_to_uri($segmenmts);
$config['base_url'] = 'controller/method/'.$uri.'/page/';
// other config here
$this->pagination->initialize($config);
I found the answers thanks to WesleyMurch leading me in the right direction. In order to always have the page variable as the last in the uri (which is necessary when using CI's pagination library), I used this
$totalseg = $this->uri->total_segments();
$config['uri_segment'] = $totalseg;
then following WesleyMurch's idea, I rebuilt the base_url,
$segments = $this->uri->uri_to_assoc();
unset($segments['page']); //so page doesn't show up twice
$uri = $this->uri->assoc_to_uri($segments);
$config['base_url'] = site_url()."/controller/method/".$uri."/page/";
and of course initialize the pagination with all the correct config options
$this->pagination->initialize($config);
I use the answer of ejfrancis but...
If for some reason the user put not numeric or negative number in the url's page var, i suggest make a validation before set the $config['uri_segment'], like this one:
$totalseg = $this->uri->segment($totalseg)>0 &&
is_numeric($this->uri->segment($totalseg))?
$totalseg : NULL;
I hope it help!

Codeigniter problem with routes!

I am trying to do this route trick:
$route['cp/roles/:num'] = "cp/roles/index/:num";
but it doesn't work :(
please help me!!
advanced thanks .
According to the documentation on URI Routing:
$route['product/(:num)'] = "catalog/product_lookup_by_id/$1";
“A URL with "product" as the first segment, and a number in the second will be remapped to the "catalog" class and the "product_lookup_by_id" method passing in the match as a variable to the function.”
So, for your particular instance, you would do the following:
$route['cp/roles/(:num)'] = "cp/roles/index/$1";
You could try
$route['cp/roles/:num'] = "cp/roles";
and then instead of passing a variable in your function you use
$this->uri->segment(3);
or the number that correspond to the segment.

Resources