1

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']);
    }
}
Skeptical Ant
  • 384
  • 5
  • 19
  • what else do you have in the view? and in the controller? From what you show, it's weird to behave in that way... – Chemaclass Feb 18 '20 at 09:35
  • We need more information. That kind of behaviour can be caused by what's in your .htaccess, or in the header. One thing you can try is setting the base url in the base element – Michael Beeson Feb 18 '20 at 09:44
  • @Chemaclass I will add the controller class and the router as well. – Skeptical Ant Feb 18 '20 at 11:01

1 Answers1

1

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

Martin
  • 22,212
  • 11
  • 70
  • 132