Home » Php » html – How to stop HREF from adding to the url path. linking to local files in PHP

html – How to stop HREF from adding to the url path. linking to local files in PHP

Posted by: admin February 25, 2020 Leave a comment

Questions:

My code is really straight forward but the problem is whenever I click on an anchor tag
it will automatically append to the url path resulting in a page that does not exist.
For example

 <li> <a href="blog/tag?cat=<?= $singleTag['id'];?>" 
       class="d-flex text-capitalize">
                           <p><?= $singleTag['name']; ?></p>
       </a>  </li>

It will assume that I’m trying to navigate to

blog/blog/tag?cat=something

I really don’t know where the first /blog
came from I looked into other answers and they were suggesting
to add // dobule back slashes infront of the link but that will not work since I’m trying to refer to a local file.

My routes file

<?php

$router->get('','controllers/index.php');
$router->get('about','controllers/about.php');
$router->get('contact','controllers/contact.php');
$router->get('events','controllers/events.php');
$router->get('event/detail','controllers/event-detail.php');
$router->get('news','controllers/news.php');
$router->get('news/detail','controllers/news-detail.php');
$router->get('members','controllers/members.php');
$router->get('blog','controllers/blog.php');
$router->get('blog/detail','controllers/single-blog.php');
 $router->get('blog/tag','controllers/all-blog-tags.php');
$router->get('oops','controllers/error-page.php');    ?>

My request class

<?php

class Request
{
    public static function uri()
    {
      //  dd(parse_url($_SERVER['REQUEST_URI'],PHP_URL_QUERY));
      $urlQuery = parse_url($_SERVER['REQUEST_URI'],PHP_URL_QUERY);
       if (strpos($urlQuery, 'slug=') !== false) {
       $GLOBALS['slug'] = $urlQuery;

       }
       if (strpos($urlQuery, 'cat=') !== false) {
        $GLOBALS['cat'] = $urlQuery;

        }

          return ( trim(parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH),'/'));
     }

    public static function method()
    {
        return ($_SERVER['REQUEST_METHOD']);
    }
}                ?>

My router class

<?php

class Request
{
    public static function uri()
    {
      //  dd(parse_url($_SERVER['REQUEST_URI'],PHP_URL_QUERY));
      $urlQuery = parse_url($_SERVER['REQUEST_URI'],PHP_URL_QUERY);
       if (strpos($urlQuery, 'slug=') !== false) {
       $GLOBALS['slug'] = $urlQuery;

       }


          return ( trim(parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH),'/'));
     }

    public static function method()
    {
        return ($_SERVER['REQUEST_METHOD']);
    }
}
How to&Answers:

href="/blog/tag will start at the root of the public_html space on a server/local machine. For remote files this is the correct way to do it.

href="/blog/tag
      ^
      |

Note the leading / to tell the browser to start at the local filepath root, so the browser will begin at

site.com/ <== this / is the leading one, above.

Therefore:

/blog/tag

Will ALWAYS be:

your-website.com/blog/tag

Without the leading slash, the path is relative to the current file, not to the website root.

Also browse this Q&A