PHP MVC - Inclusion of CSS, Images etc

4

Hello, in my application I'm having problems when I add a css file, image or any other file that is frontend, because when I include it, I need to set the whole directory of it, for example.

The views directory has the following structure

  
  • App / Controller /
  •   
  • App / Model /
  •   
  • App / View / Templates / Template_name (Ex: default)
  •   
  • Index.php
  •   

But when I include css for example, I have to set the entire directory of the templates, type.

What I want is to be able to include without pointing the entire directory. Type, from / templates / default /...

    
asked by anonymous 23.03.2014 / 15:21

4 answers

4

mod_rewrite / .htaccess

Enable / install mod_rewrite on your apache server .

Configure your .htaccess

Follow the template below:

(I made the configuration for css only, but you can customize this in 'n' ways)

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteRule ^css/(.*)$ /app/view/templates/default/frontend/css/$1 [NC,L]

What should you modify in HTML?

Only this:

<link rel="stylesheet" href="/css/styles.css">

And that's it!

The .htaccess will effect the internal redirect from url /css/styles.css to the correct destination (explaining in a simpler way).

I hope I have helped.

Any questions leave a comment below.

    
23.03.2014 / 20:45
1

We have here in the company an application in the MVC model. What we did was create a php file with all the settings and one of them is the folder with the styles.

<?php
define("CSS_DIR","/app/view/templates/default/frontend/css/");
//outras definições
?>

We make the file include above in the index, before making the call to css. Then we reference the constant that we created as follows:

<link href="<?=CSS_DIR.'nome_do_css.css';?>" rel="stylesheet">

Is it possible to do with htaccess too, do you know how to work with htaccess?

    
23.03.2014 / 16:04
1

In my applications I simply create a public files folder, separate from the rest of the structure, and create folders for each project. Something similar to this:

  • public
    • css
      • project 1
      • project 2
      • project n
    • img
      • project 1
      • project 2
      • project n
    • js
      • project 1
      • project 2
      • project n
  • src
  • vendor

From this, simply reference the files directly, using a slash as a prefix to avoid problems with url-rewrite if apache's mod_rewrite is enabled (or any equivalent)

<link rel="stylesheet" type="text/css" href="/css/projeto1/estilo.css" />
<script type="text/javascript" src="/js/projeto1/arquivo.js"></script>
<img src="/img/projeto1/icone.gif" alt="icone" />

If you need to do what twig can open these files, just create a mapping for the public folder you want. This makes everything much simpler.

    
04.04.2014 / 19:55
1

Exactly what you want, mod_rewrite solves the question as Patrick replied, but by elaborating a bit more, .htaccess might not be considered the "smart" option, and not all php web servers work from Similarly, you can for example use other alternatives such as a symbolic link , both Windows and Linux support it, even if you do not have direct access to the server, ie if shared, you can do with a PHP script, or a full PHP option, as I do in my framework that you can adapt to yours.

link

In my HMVC framework, what I do is have in the view the path of the view, which allows me to define paths in a generic way like this:

view /examples/helloworld/views/hello.php

<script src="<?= $view_path ?>js/hello.js">

Application / Module: helloworld
View: hello

And the path will be:

<script src="examples/helloworld/views/js/hello.js"></script>

While not exactly your need, to remove the 'examples / helloworld / views /' you can do as previously said create a symbolic link, or else the css or js directory could be, without any problem defined in the same path than the index.php.

So each MVC triad is allowed to encapsulate its specific resources.

link

    
26.04.2014 / 18:18