Best way/practice to ensure links are going to proper location when not on root of domain? - html

I've been wondering this for a while now, but what is the best way to ensure that in a web app (RoR, Sinatra, PHP, anything) that when you are creating links (either generating with a method, or writing in by hand) that they go to the proper place whether you are on the root of a domain or not: http://www.example.com/ or http://www.example.com/this/is/where/the/app/is/
My thoughts are get the end-user to specify a document root somewhere in the config of your app, and use that, however I'm trying to think of a nice way to do it without the end-user having to configure anything.
Edit: By end-user, I mean the person setting up the application on a server.
Edit: I can use the beginning '/' to always get the link relative to the domain, but the problem is what if the app itself is not at the root, but some place like http://www.example.com/this/is/where/the/app/is/ so i want to say gen_link('/') and have it return /this/is/where/the/app/is/ or gen_link('/some/thing') and return /this/is/where/the/app/is/some/thing

How about trying to set the base element in the head of you html layout?
First, get the URL, eg. in a way Ilya suggests (if PHP is OK for you). After that you can use the base tag as follows:
<base href="<?= $full_site_url ?>" />
That will set the default URL for all the links and the browser will prepend it to every relative link on the page.

First of all you need to route all your urls through some kind of url re-writer function.
So you no longer do:
Foo
But instead something like:
Foo
All the web frameworks out there have a function like this. While they usually do all kinds of magic in there (to do with MVC controller paths and views and what not), at the end of the function (conceptually) they all prepend your url with a "root" (eg "/this/is/where/the/app/is/"), so as to allow you to create urls in your application that are independent of a hard-coded base path.
RoR uses a configuration directive called "relative_url_root".
Symfony (php) uses a configuration directive also called "relative_url_root".
CakePHP uses a configuration directive called "WEBROOT_DIR".
In cases where these frameworks are running on Apache, this value is often calculated dynamically (if you haven't set it explicitly). On other webservers the environment variables are often not available or are incorrect so this value cannot be determined consistently.

ilya's answer is a good one, but I think a simpler way to do this is just to precede all your links with a leading "/". This will ensure that they are always relative to the root of the domain:
Something <!-- Always links to www.domain.com/some/thing -->
Something <!-- Acutal destination depends current path -->

You can determine everything you need yourself, no need for configs.
Here’s a PHP example (let’s say index.php is your script name):
<?
$folder_on_server = substr ($_SERVER['PHP_SELF'], 0, strpos ($_SERVER['PHP_SELF'], '/index.php'));
$server_name = $_SERVER['SERVER_NAME'];
if (80 != $_SERVER['SERVER_PORT']) {
$server_name .= ':'. $_SERVER['SERVER_PORT'];
}
$full_site_url = 'http://'. $server_name . $folder_on_server;
?>
Now, you can always make a link like this:
Something
See also discussion in comments.

Related

Where YII_DEBUG can be defined?

By default Yii2 generates file web/index.php with defined('YII_DEBUG') or define('YII_DEBUG', true); line. It's entry point of any page on site. And this the first line of a code. So checking for defined YII_DEBUG seems meaningless. I suppose this constant can be defined in something else place. But can't find where to do it.
In my personal case I have a local version of site and want to enable this constand for debugging purposes but don't want to change web/index.php. This file is under VCS (git) and I don't want to accidentally enabled debug in production.
StackOverflow has allready similar question. But it targeted on other sense and didn't give answer on my question. So I just created a new question.
Even if the index.php is default entry point for Yii app, you can still create your own entry point, include the index.php in it and set web server to use that file instead of index.php.
For example you can create custom-entry.php like this:
<?php
define('YII_DEBUG', false);
// do something ...
require index.php;
Or you can define YII_DEBUG in some script that is run at start of each request by auto_prepend_file directive.
But those are not exactly best options how to deal with your case.
In your case I would suggest to simply set your versioning system to ignore local changes of index.php file. For example if you are using GIT you can use skip-worktree flag to do that. I don't know CVS much so I'm not sure how exactly it is done with that.
Yii does it like that
defined('YII_DEBUG') or define('YII_DEBUG', true);
which means that if it's not defined already - define it.
This is a proper place to define it. The above statement is just in case somehow you got this already defined by any mean which Yii will honor.

How to define variables in phpstorm

In my PHP project I'm using static-file server (ex img.domain.com).
That server path I have is stored in $config table. ($this->config['file_server']).
During developement static server-file is local server (main root).
Problem is that when using it I don't get path suggestions (ctrl+space) eg:
<img src="<?=$this->config['file_server'];?>image.png">
Can I make phpstorm to interprate $this->config['file_server'] as "/" sign?
I've asked the same question on jetbrains forum: http://devnet.jetbrains.com/message/5515452#5515452
Anwser from Andriy Bazanov"
Hi there, You cannot.
The actual inspection is disabled in such
situations on purpose (to avoid false warnings .. since it is unknown
in advance what that path would be). IDE currently does not allow to
provide value or return type for individual array elements anyway On
another hand -- it works just fine if leading "/" is actually present,
i.e. config['file_server'];?>/image.png"> (in such
case path completion works)

How can I create PHP templates for PhpStorm with existing code inside?

I want to create a php Template in PhpStorm with has a comment block at the top with the usual info (author, creation date, class etc.) but also with a bunch of premade functions.
The purpose of this is that I want to make PHP Unit Class Template with the setup/teardown functions already coded, because these template are used for one project I don't expect they will change as the setups just set global which really should always be set up to make building the test easier (i.e. getting global scoped helpers).
I've tried creating the file templates however when I've copied the code into the template, any variable comes up as in input box when I go to create the file, which might be fine for me using but for someone who's using it for the first time they can screw up by filling in values for this-.
So I am wondering, how can I create a template in PhpStorm which has code in it?
You need to escape $ character which is used by Velocity template engine internally (has special meaning).
You can use ${DS} or \$ for that; so $this will become ${DS}this or \$this .
P.S. ${DS} is a safer choice overall as in some cases \$ may not work.
The official help page has it all explained: https://www.jetbrains.com/help/phpstorm/file-template-variables.html

How to get the current Skin's file path in Mediawiki?

In MediaWiki skin definitions, the BaseTemplate that gets extended has several attributes for creating links to other pages in the wiki, but I've got a situation where I need the path to the skin's directory, to pull some images used to create the UI. By default, that would just be /skins/mySkin/images/foo.png, by default but if someone changes the $wgStylePath variable, or renames the skin, that would be an issue. Is there an existing variable that has that URL build out, hidden somewhere in the BaseTemplate methods?
Yes, the SkinTemplate class, which contains the code to set up the template variables before executing the template, provides access to $wgStylePath via the 'stylepath' template variable.
When you subclass SkinTemplate to define your skin's main class, you are also expected to override the $stylename member variable, which specifies the subdirectory under which your skin's own stylesheets and images reside. (This would usually be the same as the name of your skin in lower case, but it doesn't have to be; it's perfectly fine to have, say, two related skins using images from the same directory.) This is also made available as a template variable, surprisingly named 'stylename'. So one way to specify an image path in your template would be something like:
<?php $this->text('stylepath') ?>/<?php $this->text('stylename') ?>/images/foo.png
Another way, (formerly) used e.g. by the Vector skin, is to use the getSkinStylePath() method from the Skin class (which is the superclass of SkinTemplate; it's kind of messy and tangled for historical reasons, but basically you can pretty much treat them as one class split into two files).
Update: As of MediaWiki 1.36, getSkinStylePath() has been deprecated. The recommended alternative, according to the release notes, is to "replace usages with the direct path to the resources."
To use it, you pass in the name of the file as a parameter, and the method automatically prepends $wgStylePath and $stylename to it (and appends $wgStyleVersion as a query string). Note that this is not a template method, so you have to escape and print the returned URL yourself:
<?php echo htmlspecialchars( $this->getSkin()->getSkinStylePath( 'images/foo.png' ) ) ?>
There's also a getCommonStylePath() method which does exactly the same thing, except that it uses the string "common" instead of $stylename.
Apparently this is the new way:
$this->getSkin()->getConfig()->get( 'StylePath' ) . '/SkinName/images/foo.png';
Source: https://phabricator.wikimedia.org/T270754

SSI - test if a file exists

I'm dynamically adding ssi includes based on variables and I would like to be able to have a default include in case a file doesn't exist. ie:
if /file/testthisfile.ssi exists
add /file/testthisfile.ssi
else
add /file/default.ssi
Is this possible?
Thanks!
No - I was afraid of that answer. But for anyone who might come across this question in the future I did find a work around for simple cases. You can edit the error message and in my case, output an image:
<!--#config errmsg="<img src='/file/testthisfile.jpg' alt='' />" -->
So if the file doesn't exist you can set a default.
Must underline that this will only work for simple cases, but it's a nice little work around!
Actually contrary to the answers here, SSI does in fact support file existence tests. this is the syntax
<!--#if expr="-A /private" -->
Click here to access private information.
<!--#endif -->
Support for the -A flag may need to be enabled in your apache configuration.
The expressions used in this spot of SSI have been factored out into an apache expressions module documented here
http://httpd.apache.org/docs/current/expr.html
but the -A flag is also available in "legacy" SSI expression parsers.
SSI does not support file detection.
I thought about this for a while, and indeed, ahgood was correct, SSI does not have a built-in file detection function, so flow control is limited.
As an aside, I did find a reference to an extended version of SSI (a VMS based system)
http://wasd.vsm.com.au/doc/env/env_0400.html
and there were some extensions that would allow you to check for file existence in some sort of a fashion.
However, more often then not, if one were using SSI, one would probably be running in a LAMP environment, so one could take advantage of SSI's ability to run a CGI/PHP script in the include statement.
Without too much trouble, one could resort to:
<body>
<!--#include virtual="insert_intro.html" -->
<h2>Insert An Existing File</h2>
<!--#include
virtual='checkFileExists.php?fn=insert_help.html&df=insert_default.html' -->
<h2>Insert a Non-Existing File</h2>
<!--#include
virtual='checkFileExists.php?fn=insert_no_help.html&df=insert_default.html' -->
</body>
which uses a PHP script to do all the file checking:
<?php
$theFileName = $_GET['fn'];
$theDefault = $_GET['df'];
if ( file_exists($theFileName) === TRUE ) {
include($theFileName);
} else {
include($theDefault);
}
?>
I pass two file names, the intended file and the backup/default file, the script checks for the first and if it is not found, uses the second.
This approach begs the question, why use SSI when PHP is available? In some cases, especially in a legacy system, there may be a big website based on SSI and a work-around, though less elegant, would solve a problem.
PHP is not mandatory, a PERL script would also work.
Finally, I did experiment with trying to use PHP's apache_setenv but I could not figure out how to pass environment variables between PHP, Apache and SSI (I also tried setting $_SERVER and $_ENV variables but without success).
Assuming you are running Apache 2.4 you can use the -F option (note the quoting).
<!--#if expr='-F "/private"' -->
Click here to access private information.
<!--#endif -->
From the docs (http://httpd.apache.org/docs/current/expr.html):
True if string is a valid file, accessible via all the server's
currently-configured access controls for that path. This uses an
internal subrequest to do the check, so use it with care - it can
impact your server's performance!
For the example to work the Apache user will need access to the direcotory/flag that you are testing. You may also need the following in a .htaccess or httpd.conf file:
<Directory /private>
Require all granted
</Directory>
You can do it, like this:
<!--#include virtual="/file/testthisfile.ssi" onerror="/file/default.ssi" -->
Please note that "-F" unary operator, as well as "-A" unary operator, only refer to path accessibility and not to actual existence of the resource.
Have a look here: http://httpd.apache.org/docs/2.4/expr.html (Unary operators).
Operators performing such task (-e, -s, -f) are not available under mod_include.