I have a custom component I'm working on and I'm writing an import script which is running in the administration area. I have the following code:
$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
$newUrl = JRoute::_($newUrl);
the first part works returning similiar to this:
index.php?option=com_content&view=article&id=45:joomla-sociable-and-sharethis-module&catid=18
the second part shows it like this:
/administrator/index.php?option=com_content&view=article&id=45:joomla-sociable-and-sharethis-module&catid=18
Both of the above urls are as you'd expect the component com_content to render these urls as if I wanted to use them within the administration area.
Any idea how to force JRoute to work as it would when used in the frontend?
NB: This is being used within a controller of my component, if it makes any difference and I'm including require_once (JPATH_SITE . '/components/com_content/helpers/route.php');
For those who find this on Google and struggle with using JRoute::_() and contentHelper::getArticleRoute().
$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
// better will be check if SEF option is enable!
$router = new JRouterSite(array('mode'=>JROUTER_MODE_SEF));
$newUrl = $router->build($newUrl)->toString(array('path', 'query', 'fragment'));
// SEF URL !
$newUrl = str_replace('/administrator/', '', $newUrl);
//and now the tidying, as Joomlas JRoute makes a cockup of the urls.
$newUrl = str_replace('component/content/article/', '', $newUrl);
Here's a snippet that will work for Joomla 3.6
$routerOptions = [];
if (JFactory::getConfig()->get('sef')) {
$routerOptions['mode'] = JROUTER_MODE_SEF;
}
$siteRouter = JRouter::getInstance('site', $routerOptions);
$link = $siteRouter->build($yourRoute)->toString();
$link = preg_replace('#^/administrator#', '', $link);
A nicer solution would be to create a new router instance, so, the code will be something like this:
$app = JApplication::getInstance('site');
$router = &$app->getRouter();
$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
$newUrl = $router->build($newUrl);
$parsed_url = $newUrl->toString();
$parsed_url = str_replace('/administrator', '', $parsed_url);
This way you will always obtain the right URL for the item, no matter if it is a joomla article, K2 article, etc...
** Notice that depending on the type of the item ( k2, joomla, etc) , $newUrl should be obtained with the consequent method.
I think that this one would be an easier solution:
$newUrl = JRoute::_(ContentHelperRoute::getArticleRoute($import->id.':'.$import->alias, $import->catid));
This will give you the same result as the other two previous answers but with less coding.
Hope this helps.
In Joomla 3.9 they extended the JRoute class (now called Route class) to include a link() static method which solves this problem.
use Joomla\CMS\Router\Route;
$newUrl = ContentHelperRoute::getArticleRoute($import->article_id.':'.$import->alias, $import->catid);
$newUrl = Route::link("site", $newUrl);
Route::link() works just the same as Route::_() except that you can must provide the additional first parameter to specify the client you want the URL built for. See https://api.joomla.org/cms-3/classes/Joomla.CMS.Router.Route.html#method_link.
Related
If I want to link to a cms page from a template with smarty, I currently use something like this:
{$link->getPageLink('cms',null,null,'id_cms=4')}
But that is going to generate a regular url (with query string), so if I activate pretty urls (url rewrite), it won't work. I analized the Link class but I can't find a way to generate a proper rewritten url. In fact, THERE IS a simple way:
{$link->getCMSLink(4)}
BUT, taking a look into Link::getCMSLink notes, I read that using an ID instead of a CMS object is deprecated. But from a template I don't have the cms object available.
Anyone had the same problem?
I found it really by "let's try if this one works".....
In PS1.6 you can get CMS object like:
$myCMS = new CMS( YOUR_CMS_ID );
If you want to use it in tpl, you have to define it in your controller e.g.:
$this->context->smarty->assign( "myCMS", $myCMS );
This is my solution. I recently tested it for Prestashop v1.6 and v1.7
This code utilizes the method getCMSLink of the Link class. It is necessary to know the id_cms of the CMS Page, to create the object model.
$link = new Link();
$cmsPageObject = new CMS(
$id_cms,
$id_lang
);
// $cmsLink has the URL string.
$cmsLink = $link->getCMSLink(
$cmsPageObject,
null,
Configuration::get("PS_SSL_ENABLED") === "1",
$id_lang,
null,
null
);
How to get parameters from below url
domain.com/admin/edit/12
I want to access this value (12) in edit function.
I searched around but didn't found any inbuilt solution in zend framework.
Even in other framework it works easily.
Like in codeIgnitor it works as segment and function parameter.
How I can see from code , you have Admin_Controller & some action edit ( by default routing) . For getting edit value , U need generate urls like domain.com/admin/edit/id/12/ (for example) . And than in action edit use next:
$id = $this->_request->getParam('id',0);
if ($id){
//get info for edit by ID
}
EDIT
IF you still want urls like domain.com/admin/edit/12, do next:
$uri = $this->_request->getRequestUri(); // or $this->getRequest()->getRequestUri()
$id = intval(end(explode('/',$uri)));
if($id){
// do something
}
I am extending the newsletter area so that I can send out an email with a list of products but I can't find out how to get the seo url for each product?
When I do this I actually end up with the admin URL.
$this->url->link('product/product', 'product_id=' . $row['product_id'])
This same code in the store front works fine and returns the seo url if it exists.
The only way I can get it to work is if I manually build the URL, obviously no seo url.
HTTP_CATALOG .'index.php?route=product/product&product_id='.$row['product_id']
Looking into this further I see that the admin area is missing the following code but I can't figure out how this actually works and ties in with $this->url->link so that I can modify it to work for me.
$controller->addPreAction(new Action('common/seo_url'));
UPDATE - in the end the easiest solution was to add my own method like this:
public function getUrl($route, $key, $value){
$url = "index.php?route={$route}&{$key}={$value}";
if($this->config->get('config_seo_url')){
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
if($query->row['keyword']){
$url = $query->row['keyword'];
}
}
return $url;
}
Front-end links
The best way to make a front-end link from the Admin area without modifying core files is to create your own URL object:
$url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
To create URLs you then use $url instead of $this->url, so your example would be:
$url->link('product/product', 'product_id=' . $row['product_id']);
SEO URLs
Getting SEO URLs is a bit harder and unfortunately requires copying some code or a bit of bodging.
Copied Code
Basically, copy the entire rewrite($link) function from catalog/controller/common/seo_url.php and add it to the controller class that you're creating. Then add the following line after the new $url line mentioned above:
$url->addRewrite($this);
The current controller is then used as a rewriter and all URLs get rewritten. You might be able to use a separate class, but then there are dependencies to get the database access. This seemed the most direct way of doing it, even if it is ugly.
Your code should then be:
<?php
class ControllerMyController extends Controller {
public function index() {
...
$url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
if ($this->config->get('config_seo_url')) {
$url->addRewrite($this);
}
$url->link('product/product', 'product_id=' . $row['product_id']);
...
}
public function rewrite($link) {
... [stuff from seo_url.php] ...
}
}
and you'll get SEO'd URLs from the admin area.
Bodging
If you're happy with an arbitrary require statement, then you can alternatively do the following and use the existing SEO code (which means that it'll stay up-to-date if it changes, but will fail if the file moves).
To create SEO URLs this way your code needs to be:
$url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
if ($this->config->get('config_seo_url')) {
// Require the SEO file directly - path is relative to /admin/index.php
require_once('../catalog/controller/common/seo_url.php');
$rewriter = new ControllerCommonSeoUrl($this->registry);
$url->addRewrite($rewriter);
}
$url->link('product/product', 'product_id=' . $row['product_id']);
Administration part do not need the preAction for SEO, as there are no SEO links in administration.
The problem You are getting is because on frontend the constant HTTP[S]_SERVER points to http[s]://mydomain.com/ while in administration the same constant points to http[s]://mydomain.com/admin/.
What are You looking for is maybe an edit of url class (system/library/url.php) and adding a new method frontend_link by copying link method and replacing HTTP[S]_SERVER constant in the method body by HTTP[S]_CATALOG constant that is present only in administration...
Expanding a little on #IBBoard's very informative answer: in Opencart 3.0.2 and above, seo_url.php is no longer in the catalog/common folder. It was moved to catalog/startup.
Also, it is highly recommended to run the filename in the require_once statement through opencart's modification function, like:
require_once modification(DIR_CATALOG . 'controller/startup/seo_url.php');
I'd like to know which is the cleanest way to insert an url in an email sent by Moodle module.
So far I'm using this formula, what IMHO I don't think is the cleanest way:
$url = $CFG->wwwroot.'/mod/<mymodulename>/view.php?id='.$cm->id;
The things I don't like here are:
Using $CFG->wwwroot
/mod/<mymodulename> needs to be provided always. (Assume here that I'm using a constant instead of a hardcoded string).
I expected Moodle to have a function to provide this out of the box just when providing module script. I've tried moodle_url but this function doesn't provide the path to the php script when used this way:
new moodle_url('view.php?id='.$cm->id);
I just get:
view.php?id=XX
Thanks in advance.
I would do it like this
$url = new moodle_url('/mod/<mymodulename>/view.php', array('id' => $cm->id));
echo html_writer::link($url, get_string('linktitle', 'mod_mymodulename'));
You can use following statement:
This is Absolute path of file
$url = new moodle_url($CFG->wwwroot.'/mod//view.php', array('id' => $cm->id));
I have a problem concerning url-rewriting in wordpress. I am currently working on a language plugin (nearly finished also) and as a last thing, I would like to see every url altered so that it contains the current language that has been selected by the user (or a default language if the user hasn't changed the language).
I don't have a problem altering the links, the problem lies with the rewriting done by the server. Below you can find how I change the links.
public function register_filters()
{
add_filter('page_link', array(get_class(),'alter_permalink'));
add_filter('post_link', array(get_class(),'alter_permalink'));
}
public function alter_permalink($permalink)
{
$permalink = str_replace(get_option('home'), '', $permalink);
$permalink = trim($permalink, '/');
//The next line is actually a method that is being called,
//but it will return a string like this.
$lang = 'EN';
return get_option('home') . '/' . $lang . '/' . $permalink;
//This returns a link that looks something like this:
//http://somedomain.com/EN/permalink-structure
}
So as you can see, I have no problems creating the links, the problem lies with the url-rewriting on the server itself.
I have tried this method: http://shibashake.com/wordpress-theme/wordpress-permalink-add
but I didn't get that one to work either. The problem is that I just don't seem to understand how these rewriting-rules work and that I can't seem to find a decent tutorial on the subject either.
Any help would be greatly appreciated.
the filter to listen to for handling url is request.
function request_handler($vars) {
//modified $vars here
return $vars;
}
add_filter('request', 'request_handler', 11);
i have written a plugin for customize url in wp. check out the source and see how i handle it.
http://wordpress.org/extend/plugins/auto-url/