C
C
Cyril2018-03-15 22:35:25
CodeIgniter
Cyril, 2018-03-15 22:35:25

Is it good practice to use CodeIgniter's base_url() in Views?

The bottom line is that a web application based on the CodeIgniter PHP framework first lay in the root of the site, but then was asked to remove it to the subfolder subfolder . That is, the application was in /www , and they asked to move it to /www/subfolder .
In the config.php config, I changed the base_url parameter:

$config['base_url'] = 'http://domain.com/subfolder/';

And also I configured the correct routing in .htaccess .
The problem is that when I entered the web application by typing the domain.com/subfolder/ address, all the Views resources began to load with the wrong paths. These paths have remained the same and have not changed. Also, the paths in the action="" attributes of the forms remained absolute. Like /login, /logout and so on.
I solved the problem by replacing all absolute paths in all Views with the base_url() function .
That is, for example, the CSS file was connected like this: And changed to:
<link href="/css/main.css" rel="stylesheet">
<link href="<?= base_url('css/main.css') ?>" rel="stylesheet">

Or for example, there was a login form:
<form id="user-login" action="/login" method="post">
...
</form>

And I changed to:
<form id="user-login" action="<?= base_url('login') ?>" method="post">
...
</form>

Is such a replacement in all Views considered an acceptable (correct) practice? Do they do it at all?
Or are there other ways to do it without replacement? After all, all paths are hardcoded to absolute ..
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dmitriy, 2018-03-16
@dmitriylanets

Yes

A
Artem Spiridonov, 2018-03-16
@customtema

Is such a replacement in all Views considered an acceptable (correct) practice? Do they do it at all?
Yes. But it's painful.
Remove the slash from href="/* and src="/* - so that the web server looks for files in the current directory.
Does the editor have an autocorrect feature for all files in a subdirectory?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question