PHP MVC - Adding CSS, Images etc

Asked

Viewed 1,944 times

4

Hello, in my application I am having problems when I will add a css file, image or any other file that is frontend, because when I will include I need to set the whole directory of the same, 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’m going to include for example css, I have to set the whole directory of templates, like.

inserir a descrição da imagem aqui

What I want is to be able to include without having to point out the whole directory. Type, from /templates/default/...

  • Be more specific about what the problem is, as it is very vague.

  • I forgot to finish after adding the image, forgive me. rs

5 answers

6


mod_rewrite / . htaccess

Enable/install the mod_rewrite in his apache server.

Configure your . htaccess

Follow the model below:

(I made the configuration only for css, but it is possible to customize it from 'n' ways)

Options +FollowSymLinks -MultiViews
RewriteEngine on

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

What you should modify in HTML?

Only that:

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

And just like that!

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


I hope I’ve helped.

Any doubt leave a comment below.

  • Worked as expected?

  • I think you understand, the logic is that right there, but I made a change here and it didn’t work. Rewriterule templates/(.+)$ /app/view/templates/ [NC,L] .

  • The directory you mentioned above is different. It should be in case: /app/view/templates/default/frontend/css/$1 [NC,L]. The route you mentioned above is incorrect and the parameter is missing $1 in the end.

  • Rewriterule templates/(.+)$ /app/view/templates/$1 [NC,L]. So the error continues, what I wanted is to be able to add from the templates, for example. /templates/default/.. Got it? rs, I cannot enter the default in htaccess because it is a skins system. ^^

  • 1

    In this case it is no longer related to redirection or. htaccess, what your application should do is return the skin chosen by the user for example, in the session or as a configuration (file/database) of the user, and then concatenate with this URL. Kind of: <link rel="stylesheet" href="/template/<?php echo $configuracao['skin'] ?>/frontend/css/estilos.css"> (is just one example)

  • Yes, I managed it here. rs But in relation to managing the templates I’m still seeing if saved in the database or something. rs

  • I found your answer very interesting, I didn’t know you could do this with the Assets. Could you explain to me better about the object of that line for that context that was applied? Options +Followsymlinks -Multiviews

Show 2 more comments

1

In my applications I simply create a public file folder, separate from the rest of the structure, and create folders for each project. Something like that:

  • 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, I simply refer the files directly, using a bar as a prefix to avoid problems with url-rewrite if apache’s mod_rewrite is enabled (or any other 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 it is necessary to make Twig open these files, just create a mapping to the public folder you want. It makes everything so much simpler.

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 do the above file include in index, before calling css. Then we make reference to the constant we create as follows:

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

It is possible to do with htaccess too, you know how to work with htaccess?

  • I don’t think you understand my purpose very well. rs this would then only be a define, but in the count code would still show the directory /app/view/. I wanted to make it as if the templates folder was in the system root, you know?

  • I understand, it will have to be done with htaccess, but what is the purpose?

  • Almost that where Patrick posted above. rs

  • Almost? But it was marked as the right answer, solved the problem?

  • Yes, after a few tests here it worked. : ) The problem was when I treated the controller. rs

0

Exactly what you want, mod_rewrite solves the question as Patrick answered, but elaborating a little more, the . htaccess may not be considered the most "stylish" option, and not all php web Servers work the same, you can for example use other alternatives like, a Symbolic link, both Windows and Linux support it, even if you don’t have direct access to the server, ie, if it’s shared, you can do it with a PHP script, or a full PHP option, as I do in my framework that you can adapt to your.

http://pedrosimoes79.github.io/silverbullet/

In my HMVC framework, what I do is to have a view path 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>

Although it is not exactly your need, to remove the 'examples/helloworld/views/' one can as I said earlier create a Symbolic link, or else the css or js directory could be, no problem at all defined in the same path as index.php.

This allows each MVC to encapsulate its specific features.

http://pedrosimoes79.github.io/silverbullet/

-2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.