-2

I really did a lot of research about this but it seems like this is different from the others. So ... what I have now is a project folder todo-oop and I only want to get the exact location/url of this project, so, I want to get http://localhost/todo-oop/.

Code:

<?php
  $project_folder = explode('\\', dirname(__FILE__));
  $root_directory = $_SERVER['SERVER_NAME'] . '/' . end($project_folder);
?>

Output:

localhost/partials/css/todo.css

If I check the view source and click the url it returns a wrong url: http://localhost/todo-oop/localhost/partials/css/todo.css

I want to achieve like how laravel handles their css/js/img files using asset() helper function.

Blues Clues
  • 1,694
  • 3
  • 31
  • 72
  • $_SERVER['DOCUMENT_ROOT'] –  May 31 '18 at 03:30
  • @DevinGray Hi . It will return `C:/wamp/www` and it's not what I want. – Blues Clues May 31 '18 at 03:34
  • $_SERVER['PHP_SELF'] will return exact path, but it's not super secure. You can preg replace `C:\wamp\www` with your server name –  May 31 '18 at 03:35
  • you want to get `http://localhost/todo-oop/` based on what exactly? –  May 31 '18 at 03:37
  • @smith Sorry but I don't understand what exactly you want to ask. – Blues Clues May 31 '18 at 03:38
  • well im not sure what you wanting but here is my last guess for the day `$_SERVER['SERVER_NAME'] .dirname($_SERVER['REQUEST_URI'];` might be better with `$_SERVER['PHP_SELF']` at the end instead of request –  May 31 '18 at 03:53
  • You can't use `$_SERVER['REQUEST_URI']` because you're just getting the uri you're visiting. – Blues Clues May 31 '18 at 03:57
  • try this `$_SERVER['HTTP_HOST'].'/todo-oop/'` – xanadev May 31 '18 at 04:00
  • `dirname(XXXX)` here will return the first path element, which i think is what your asking (in a strange way) –  May 31 '18 at 04:01
  • Try `echo base_path();`. – Sakezzz May 31 '18 at 04:08
  • Why don't you hardcode your domain name as a setting (`define()`) in a config file located in the root of your domain (or in a json data object and call upon it for settings) then you have a function that can call that domain constant and combine with whatever you inject into the function? That would be easier than relying on the `$_SERVER` attributes. Then you know exactly what you will get. This concept is common place. – Rasclatt May 31 '18 at 04:11

3 Answers3

0

One of the great things of PHP is that you can read the source. If you want to know how the asset() function works, you can just read the code!

// Illuminate/Foundation/helpers.php
function asset($path, $secure = null)
{
    return app('url')->asset($path, $secure);
}

So you give the asset function a path to what you want to include. The asset function in turns gives it to the container instance to resolve (the app()) and then Laravel kicks in with a boatload of magic. In short it's just trying to resolve the correct URL to the path you've given.

Downside of Laravel is that is uses a lot of magic code to prevent boilerplate. This is often useful but can make it hard to see what's happening internally. Frameworks often have this trade-off between boilerplate and magic, where Laravel tends to forgo boilerplate whenever it can.

Now to the answer

What you can do that Laravel does pretty smartly, is to set a property/global/variable whenever someone connects to your app.

// bootstrap/app.php
$app = new Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')
);

// Illuminate/Foundation/Application
public function __construct($basePath = null) 
{
    if ($basePath) {
        $this->setBasePath($basePath);
    }
}

Now you can access this path (which is the project root) from wherever you want in your app, by calling the base_path() helper. Which does in fact return that exact path we just set:

function base_path($path = '')
{
    return app()->basePath().($path ? DIRECTORY_SEPARATOR.$path : $path);
}

Hoped this answer helped you a bit!

Loek
  • 4,037
  • 19
  • 35
0

As suggested, a simple solution is to create a config file (json, xml, or php — I'll use the last) that contains important information found inside the root of your domain, in this case the domain name:

/config.php

<?php
define('DS',DIRECTORY_SEPARATOR);
define('ROOT_DIR',__DIR__);
define('INCLUDES',ROOT_DIR.DS.'includes');
define('FUNCTIONS',INCLUDES.DS.'functions');
# Define base domain
define('SITE_URL','//localhost');

This would just combine project path (if set).

/includes/functions/asset.php

function asset($path=false)
{
    # Check if a project folder is set
    $proj     = (defined('PROJECT_NAME'))?  PROJECT_NAME : '';
    # See if the current mode uses SSL
    $protocol = (isset($_SERVER['HTTPS']))? 's' : '';
    # Create a path (if no project, it will leave "//" so you need to replace that)
    $final    = str_replace('//','/','/'.trim($proj,'/').'/'.ltrim($path,'/'));
    # Send back full url
    return "http{$protocol}:".SITE_URL.$final;
}

This is just basically what you would do.

/index.php

<?php
# Include config
require_once(__DIR__.DIRECTORY_SEPARATOR.'config.php');
# Include the asset function
include_once(FUNCTIONS.DS.'asset.php');
# Define the current project. If you are always accessing this,
# then you may want to have it in the config, but if you are doing multiple projects
# you can leave the define in the root of each project file
define('PROJECT_NAME','todo-oop');
# Use asset to write the asset path
echo asset('/partials/css/todo.css');

This should write:

http://localhost/todo-oop/partials/css/todo.css

If you have no project defined:

http://localhost/partials/css/todo.css

You can also change the SITE_URL to include the project directory if you only have one project being accessed with a solitary config:

define('SITE_URL','//localhost/todo-oop');

As demonstrated by @Loek you can use a framework do create these kind of paths and such, or you can create classes (if you are confident enough to do so) to do essentially what I am demonstrating. The benefits of using class/frameworks is that they would have more flexibility and dynamic interaction but for a very straight-forward process, you can do these types of paths using the above methodology.

Rasclatt
  • 12,498
  • 3
  • 25
  • 33
-1

Try this

<?php echo "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'].'/'; ?>

As per this answer, hope this will work.

pspatel
  • 508
  • 2
  • 7
  • 18