autocomplete does not match after using includes

hi

I am using a modular approach to PHP development where 'modules' (simple content sections are loaded through the bootstrap index.php page).

So a typical url might look like this

http://www.example.com/index.php?p=about

in PHPStorm it works well at autocompleting say a path to an image or resource like a pdf, the problem is that path is no longer accurate once that page is included somewhere else, the path becomes wrong.

How can I fix this, I can provide more detailed explanation if need be.

8 comments
Comment actions Permalink

Hi John,

You better provide better example/explanation as current one is not clear enough (at least for me).

the problem is that path is no longer accurate once that page is included somewhere else, the path becomes wrong.

Which path?


Since I'm not sure what "paths" you are talking about here, right now I may only suggest using URLs relative to the website root instead of relative to the current file. In other words instead of <img src="images/logo.png" /> use <img src="/images/logo.png" /> (notice the leading slash in the path to the logo.png)

0
Comment actions Permalink

Thank you for the fast reply!

I will test this at work tomorrow (about 12 hours from now) and will let you know.

That makes sense what you wrote and I think that will work.

Cheers!

0
Comment actions Permalink

Hi,

Sorry for my delayed response.

I wanted to talk about the problem further.

The way I build websites in PHP is a modula approach with a file structure like the following.
mod.png
So when using PHPStorm (Which I am new to and absolutley loving!), it does a great job of having dropdowns when typing in paths like an image or a document or including a file.

The problem is however it gets the path from the physical structure of the files, not the actual path once a file has been included inside another (if that makes sense).

Is this a known problem with no real solution besides not using relative paths when it is going to stuff things up? Or is there a way to 'tell' PHPStorm what the intent is?

Thanks a gain!

0
Comment actions Permalink

Sorry, but I still need an example of the actual problem, as I cannot figure out what "include" your are talking about.

0
Comment actions Permalink

Thanks for the fast response again!

Ok so here is my index.php page which acts like a bootstrap for the website.

<?php # index.php

/*
*    This is the main page.
*    This page includes the configuration file,
*    the templates, and any content-specific modules.
*/


// Require the configuration file before any PHP code:
require_once ('./includes/config.inc.php');


// Validate what page to show:
if (isset($_GET['p'])) {
    $p = $_GET['p'];
} elseif (isset($_POST['p'])) { // Forms
    $p = $_POST['p'];
} else {
    $p = NULL;
}


// Determine what page to display:
switch ($p) {


    case 'about':
        $page = 'about.inc.php';
        $page_title = 'About This Site';
        break;
    
    case 'contact':
        $page = 'contact.inc.php';
        $page_title = 'Contact John';
        break;
    
    // Default is to include the main page.
    default:
        $page = 'main.inc.php';
        $page_title = 'Site Home Page';
        break;
        
} // End of main switch.


// Make sure the file exists:
if (!file_exists('./modules/' . $page)) {
    $page = 'main.inc.php';
    $page_title = 'Site Home Page';
}


// Include the header file:
include_once ('./includes/header.html');


// Include the content-specific module:
// $page is determined from the above switch.
include ('./modules/' . $page);


// Include the footer file to complete the template:
include_once ('./includes/footer.html');


?>

A typical url for my website looks like this:

http://example.com/index.php
or
http://example.com/index.php?p=contact

in the case of the second URL it loads the contact.inc.php module inside the modules folder

So while I am inside the modules folder to add an image the path is something like

<img src="../images/image01.jpg" />

And PHPStorm is clever enough to list all the images in the images folder, but when the page loads that path is no longer correct because it is loading as index.php?p=contact and relative to index.php the path should be

<img src="images/image01.jpg" />

But if I type it like that in PHpStorm it no longer lists the images because relative to contact.inc.php the path is wrong, but once the page loads the image comes up correctly.

Hope that helps.

Thanks again!

0
Comment actions Permalink

As far as I can see 2 choices:

1. The one I have already described (my personal choice, at least for websites I do) -- URLs starts with leading slash and relative to the website root: <img src="/images/image01.jpg" />

2. In Project View (or Settings | Directories) you mark PARENT folder of images as Resource Root, then you should be able to write just <img src="images/image01.jpg" /> anywhere in the project with completion from IDE  (because internally in PhpStorm it will be resolved to that Resource Root). In this case URLs will be relative to the callign script (from browser perspective) -- in your particular example it will be relative to the index.php.

0
Comment actions Permalink

Thank you so much I will give these a go in the morning. :-)

0
Comment actions Permalink

Hey,

I just wanted to give a bit of an update to this.

Part of my problem stemed from the fact that i was working from a localhost with multiple projects, so really my URL's looked like:

http://localhost/project-name1/index.php?p=page1

So when I did as you suggested, it did work, marking that particular project as the project root, (i.e. mark project-name1 as the web root, the intellisene works fine.

I over looked the fact that my webroot was localhost, and not the specific project i was working on inside that.

Thanks again!

0

Please sign in to leave a comment.