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.