CodeIgniter Forums
View string needs escape if back slash on end - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: View string needs escape if back slash on end (/showthread.php?tid=73955)

Pages: 1 2


View string needs escape if back slash on end - InsiteFX - 06-28-2019

Not sure if this is a bug or not but while working on my adminLTE 3 module
when I have an echo view like this:

PHP Code:
echo view('Insitefx\Admin\Views\'.$slug); 

All code lines after the view are red with error: (Expecting right single quote or double quote).
As you can see it is quoted correctly, if I escape the back slash like this:

PHP Code:
echo view('Insitefx\Admin\Views\\'.$slug); 

Then everything is alright and it will load the Admin module view with no errors.

It also works correctly using just single forward slashes.

But should it also work with single back slashes? This is a Windows 10 Pro x64-bit System.


RE: View string needs escape if back slash on end - kilishan - 06-28-2019

Pretty sure that's just PHP being PHP. With a backslash just before the closing single quote, the backslash is trying to be an escape character. Though it's possible it works just fine in PHP and your editor is interpreting it incorrectly. I know you need it with double-quotes, not sure with singles.


RE: View string needs escape if back slash on end - InsiteFX - 06-28-2019

Ok, Thanks Lonnie I was just checking on it.


RE: View string needs escape if back slash on end - MGatner - 06-28-2019

You need it with both - always looks awkward but the only way to avoid escaping it.


RE: View string needs escape if back slash on end - InsiteFX - 06-29-2019

I ended up solving it this way and it works fine.

PHP Code:
echo view('Insitefx\Admin\Views'.DIRECTORY_SEPARATOR.$slug); 



RE: View string needs escape if back slash on end - kilishan - 06-30-2019

(06-29-2019, 08:55 AM)InsiteFX Wrote: I ended up solving it this way and it works fine.

PHP Code:
echo view('Insitefx\Admin\Views'.DIRECTORY_SEPARATOR.$slug); 

I think that will only work on Windows servers, no? Directory separator on *nix boxes is a forward slash....


RE: View string needs escape if back slash on end - InsiteFX - 07-01-2019

You know I think you are right because this is a Windows 10 Pro x64 system.


RE: View string needs escape if back slash on end - InsiteFX - 07-02-2019

So it would be best just to escape it Lonnie?


RE: View string needs escape if back slash on end - MGatner - 07-02-2019

Escaping it is a fine solution. There's really no other way to deal with a trailing backslash unless you want to define your own constant or variable, but then you'll still need to escape it:

$namespaceDivider = '\\';


RE: View string needs escape if back slash on end - dave friend - 07-03-2019

This reference implies that forward slashes should not be an issue which, if true, should allow use of DIRECTORY_SEPARATOR.

On the other hand, there is this bit of advice.