4

I'm developing a site with Git and Master Config, and up until yesterday I was forcing users to log-in to view the site on the development server with this code in my document head template:

{if logged_out}
    {redirect='tmp/login'}
{/if}

But now the site has been launched to production, I have to remove that code and template group. But since everything is synced up using Git, the development environment won't be password protected either.

I'd prefer to use EE's built-in login system over say a .htpasswd file if possible. I figure I can .gitignore the tmp template group so it only exists on development. Is there a Master Config variable I can use to only parse the logged_out conditional in the development environment? Like:

{if ENV == 'dev'}
    {if logged_out}
       {redirect='tmp/login'}
    {/if}
{/if}

5 Answers 5

5

Version 1.1.1 of the Master Config added two global variables by default. Here's a sample of use:

{if global:env == 'dev'}
    {if logged_out}
        {redirect='tmp/login'}
    {/if}
{/if}

An alternate approach without nesting conditionals:

{if global:env == 'dev' && logged_out}
    {redirect='tmp/login'}
{/if}

If you are using a version before 1.1.1 you can just add that section of code to your current config.master.php file.

I figure I can .gitignore the tmp template group so it only exists on development.

I'd recommend adding a conditional to any template in that group. The conditional would redirect the user if they aren't in the desired environment. It's a bigger process depending on how many templates you're running in the group, but allows you to track the template(s) and their changes. It also means your other contributors will have these files.

Example:

 {if global:env != 'dev'}{redirect='/'}{/if}
1
  • That's a great suggestion regarding the template group, I think I'll do that.
    – kmgdev
    Jan 15, 2013 at 19:37
3

Jason Siffring at Surprise Highway posted an article on how to do this with .htaccess. Unfortunately it requires a .htpasswd file, which it looks like you're trying to avoid.

Here's the link in case it's useful: http://surprisehighway.com/article/how-to-password-protect-development-and-staging-sites-with-htaccess

2

The main Master Config file has global variables configured that you can use:

From line 253:

$master_global = array(
    'global:env' => ENV,
    'global:env_full' => ENV_FULL
);

The variables for each environment are set in the config.env.php file.

{global:env} will get you "prod, stage, dev or local" and {global:env_full} will get you "Production, Staging, Development or Local".

These are global variables you can access in your templates.

So this conditional will get you what you need:

{if "{global:env}" != 'dev'}{redirect='/'}{/if}

I'd set a global variable in the config.php file:

$assign_to_config['global_vars']['ENV'] == 'dev';  

Then use this in your template:

{if "{ENV}" == 'dev'}

2
  • Do you know how I'd adapt that to work with Master Config?
    – kmgdev
    Jan 15, 2013 at 18:48
  • I just edited my answer. A global variable already exists for you to use in Master Config.
    – Anna_MediaGirl
    Jan 15, 2013 at 23:08
2

I would suggest putting site offline (via EE preferences or config file) during development and using Smart Switch to allow limited access

1

What about using EE's built-in template permissions? It's located under the "access" tab when editing a template. Way easier IMHO than writing a bunch of conditionals into your templates and then having to rip them out when going live. Here's an example:

enter image description here

I would link to the page in EllisLab's docs, but the EE docs are down right now.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.