Php does not load Css

0

Good people, I'm making a very simple page, to return to programming.

Here's the problem: my php is not loading Css, Img, or Js.

Yes, I've tried to put the absolute / relative path

My directories:

  • test /
    • public /
      • css
      • js
    • src /
      • header.php
    • index.php

  • I happen to run your "php -s -s localhost: 8000 index.php" and try to access the website, it will load the normal page, however without any css and no js.

    In my "index.php" an include of the "header.php" occurs, including the css is inserted through the html tag:

    <link rel="stylesheet" type="text/css" href="/public/css.css" />
    

    I've tried it too:

    <link rel="stylesheet" type="text/css" href="../public/css.css" />
    

    I also tried ridiculous things like putting any path to see if it gave:

    <link rel="stylesheet" type="text/css" href="css.css" />
    

    I've also tried the relative:

    <link rel="stylesheet" type="text/css" href="public/css.css" />
    

    I've tried several things, the relative path does not work either. Anyway, I've been searching for a couple of days on the internet and nothing. In every forum is said to use the absolute path, but I have used it and nothing.

    In the terminal, this message appears:

    PHP 7.0.19-1 + deb.sury.org ~ xenial + 1 Development Server started at Sat May 13 17:14:49 2017

    [Sat May 13 16:49:06 2017] 127.0.0.1:51976 Invalid request (Unexpected EOF)

    I reinstalled php, apache2, and everything else

    Use or Ubuntu 16.04 LTS

    It worked with a GIANT gambiarra here folks ...

    I did something with the slim, to try to put a friendly URL, hence I made a route only to a css (As if it were a url for a page, but for the .css).

    For example, the route "/ meucss" renders the file "meucss.css" just like a page itself.

    In the HTML tag to pull the css from there I only put href="/ meucss" and it loads the css ... But does not apply the stilo, just load

    However, I have no conditions, so I have to keep putting routes for each css / img / js that I want to insert into the page ... Does anyone have a tip?

    Thanks!

        
    asked by anonymous 13.05.2017 / 22:10

    1 answer

    1

    On the built-in server when you do this

      php -S localhost:8000 index.php
    

    Actually index.php will work as a proxy, that is, you should not actually pass index.php , you should only pass something if you want to handle requests, for example simulate an Apache mod_rewrite.

    Imagine that you have a system that creates dynamic CSVs generated by a PHP, but either the URL actually looks like a .csv, something like http://site/download/2015-04-03.csv , on an Apache server the .htaccess could be something like this: / p>

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^/download/([a-z0-9\-]+)\.csv$ csv.php?date=$1 [NC]
    

    But the PHP built in server does not support this, so we have to write a php that does this, for example rewrite.php :

    <?php
    $requri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    $pathinfo = urldecode($requri);
    
    //Se o arquivo existir então não usa o rewrite
    if (is_file($pathinfo)) {
        return false;
    }
    
    //Testa com regex a rota, se falha envia false e o PHP procura um arquivo de verdade ou emite erro 404
    if (!preg_match('#^/download/([a-z0-9\-]+)\.csv#', $pathinfo, $match)) {
         return false;
    }
    
    //Se o preg_match fizer o "match" então simula a passagem do _GET e chama o csv.php
    $_GET['date'] = $match[1];
    
    require 'csv.php';
    
    So in your case the problem is that you were using index.php as a proxy, just like I did above and since I did not have return false in the proxy and it was still being rendered HTML, so when you accessed http://localhost/meu.css it actually pointed to index.php even if the URL was different.

    The php built in server already does the whole service of a server, it is not necessary to define any initial file unless you really want to control / manipulate HTTP requests and responses.

    The only thing you should point out is if you are to create a .bat or a .sh and end up running them from another directory that is different from the directory where your index.php is located, iniciarservidor.sh is something like (if it's Linux or Mac):

    #!/bin/bash
    php -S localhost:8000
    

    And then suppose you have this:

    /home/matheus/projeto
           |       |
           |       +-------- index.php
           |
           +---------------- iniciarservidor.sh
    

    If you call iniciarservidor.sh , the server will look for the files from the matheus folder and not the projeto folder, so you may prefer to point the default directory like this:

    #!/bin/bash
    php -S localhost:8000 -t /home/matheus/projeto
    

    In this way, no matter where you call iniciarservidor.sh , it will always start the server considering the /home/matheus/projeto folder as the root of your server.     

    13.05.2017 / 23:38