CodeIgniter Forums
Path problem I have while using route groups - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: General (https://forum.codeigniter.com/forumdisplay.php?fid=1)
+--- Forum: Regional User Groups (https://forum.codeigniter.com/forumdisplay.php?fid=25)
+--- Thread: Path problem I have while using route groups (/showthread.php?tid=78115)



Path problem I have while using route groups - c_rkvn - 12-02-2020

Hello, first of all I apologize for my bad English.
I am having a problem with the routes and if there will be no problem, I would like to explain by giving examples. I will give the example from a completely clean and fresh install. So there is no additional development etc.

There is a line in the "welcome_message.php" page that calls a style file from the "public" folder.
This line is:
Code:
<link rel = "stylesheet" href = "assets/css/style.css">



If I call the route as follows, the style.css file loads without problems and looks like this (example.com/assets/css/style.css)

PHP Code:
$routes-> get('welcome''Home::index'); 



But if I call the route as a group that I mentioned below, the style.css file is not loaded and the path looks like this (example.com/blog/assets/css/style.css)

PHP Code:
$routes->group('blog', function ($routes) {
    $routes->add('welcome''Home::index');
}); 


I follow such a way to avoid using base_url when calling entities. If I add "base_url ()" to the beginning of the line I call this css file, there is no problem and it calls the file in both ways without any problem, but I wanted to get your feedback because it was interesting to me. Is this a normal process? So is this what it should be or I'm making my mistake somewhere. And I wonder if there is a way to solve this without adding base_url ?

Thank you very much for your help in advance.


RE: Path problem I have while using route groups - tgix - 12-03-2020

You should not confuse the filesystem path in public/ with the request path. When you do a request to example.com/blog/ and generate a page - the request goes through public/index.php and thanks to the .htaccess rewrite instructions it works.

However - the page you generate will look like it is served from public/blog/ and will thus look for the assets directory there.

If you are serving your public/ directory from a apache virtualhost you should be able to refer to the asset path as <link rel = "stylesheet" href = "/assets/css/style.css"> (note the "/assets" path).
If not, I'd try adding the BASE tag to your pages - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base and point that to "/".
As a third option you can inject base_url() in all your templates.


RE: Path problem I have while using route groups - InsiteFX - 12-03-2020

PHP Code:
$routes->group('blog', function ($routes) {
    $routes->add('welcome''Home::index');
});

// change to
$routes->group('', function ($routes) {
    $routes->get('welcome''Home::index');
});