Whats the purpose of public url in Laravel? - url

When we setup laravel, the default url will have "public" on it.
Example : localhost/laravel/public/users
We know that we always wanted to remove the "public" in our url specially when we go to production.
I searched in the internet and looks like removing the "public" url needs some work to achieve like moving files to other folder, modifying .htaccess, etc...
My question is, whats the purpose of "public" url?
Why doesnt laravel provide an easy way of removing the "public" url..
I know Laravel is popular these days but why give beginners a headache in just removing a part of the url?
Let me know your thoughts

The public directory holds all your files that should be accessible from the outside.
This way nobody can access any other file in your application.
How do you remove the public in your URL? Well you don't "remove" it at all. Instead you should point your domain (virtual host) directly at public (this is called Document Root)
If you don't have the possibility to do that (usually because your on a shared hosting with very restricted permissions) then and only then you need to move files or use a workaround with .htaccess. However if your hoster doesn't allow you to set the document root I would consider to switch to another one...
By the way: This is not really unique to Laravel. A lot of PHP frameworks do it (Zend Framework, Symfony, etc) all have a dedicated directory for files that should be accessible - separated from the framework core and your application.
If you're having trouble setting up your development environment correctly, you should try out Laravel Homestead. It's a pre-configured Vagrant box that makes it very easy to get your site locally up and running (also comes without public in your URL!)

When you make your site's root point to the public folder, e.g. www.mysite.com points to /path/to/laravel/public, there are a few advantages.
Your files outside of public, like your .env with your passwords cannot be accessed doing things like www.mysite.com/../.env and other common "exploits" are prevented just by taking this simple approach.
It's quite a common pratice in other frameworks too, not only Laravel or PHP.

Related

MAMP/WAMP - Switch back from real project URLs to localhost in all my files

Good morning,
does anyone know how I could configure MAMP (or WAMP) in order to automatically change my project URLs, to localhost without having to search/replace inside my documents (operation I suppose to be a bit gross because possibly altering my code).
My goal is to develop on local while keeping the final and real URLs in my documents.I suppose lot of you have encountered this issue one day :)
In other word, I would like to alternate between online and local more easily.
I a beginner, please consider,
for all the biginners, here's the thing. I've created a config.php file which contains constants: one config file for the local project folder and one for the online server folder.
Inside this config file, I've create a constant (constant are then available everywhere in the project) to define the main URL of the project. e.g.:
define('CST_MAIN_URL',http://www.myproject.com); // for the online config.php file
define('CST_MAIN_URL',http://localhost:8888); // for the local config.php file
Thus, each header or redirection can work with that constant, like:
header('location:' . CST_MAIN_URL . 'index.php');
Then, things must have to do with RewriteEngine in your htaccess file, for instance whenever you must modify the behavior of MAMP/WAMP if an interrogation point or a slash provokes you with its malicious resistance. But, unfortunately RegEx expression must be understood as a basic level for mastering those url rewritings.
Hope it'll helps.

Referencing to before site root

I have a few websites that are in a single folder for example:
en
de
nl
fr...
in these folders are the websites.
What I want to do is add an images folder into here and have all of the sites referencing this images file. The reason for this is so when I add another image I only have to do it once not multiple times.
My problem is that I can't seem to find a way to reference to this file. I'm not sure if its possible becasue it is before the site roots.
Is there any way to do this?
You can either do it as a virtual directory which you include in all your sites, but a stronger approach would be just to make it a site by iteself. Chances are if you are using it for images, then its purpose is to serve static content.
What i would do is create a site named static.yoursite.com and in that site have a few folders (css img js). Then when you need to load an image from any of your sites you would do it like this:
img src="//static.yoursite.com/img/global/background.png" or
img src="//static.yoursite.com/img/de/welkome.gif" or
Having it all in a separate site might make it easier to migrate to something like S3 or a real CDN like cloudfront in the future. It will also make it super easy to set the proper expires headers and caching for static content.

ASP.Net MVC Bundle linked content files

I've been trying to reduce the amount of copying and pasting of content files across some of my projects and decided to go down the adding files as links from a central project.
The problem I have now is that the System.Web.Optimization.Bundle.AddFile(string virtualPath, bool throwIfNotExist = true) does not work as the file doesn't really exist in the directory I specified.
Does anyone have any pointers? Or maybe an alternative to linking content files?
Thanks.
I think you cannot access files outside of your web project with the virtual path system and It might hurt when you want to deploy your app.
I suggest to make a custom project for your static content with a custom domain: e.g. static.yourcompany.com and reference all this files from this domain. This also has the advantage that the browser does not have to add the cookies for authentication and tracking to these requests, which might be faster if you have a lot of traffic. You can also open your custom cdn (http://www.maxcdn.com/pricing/) or store the files in azure or amazon aws (which is more or less free for less files and traffic).
Another approach is to make some batch files to keep your content files synced.

how to use html img

I have the following folder structure in my zend framework 2 tutorial project in Ubuntu:
/zf2tutorial
/module
/Album
/view
/album
/album
myImage.png
index.phtml
in my index.phtml code I have a line such as this:
<img alt='SMALLIMG' src='myImage.png'/>
this does not work.
If I change the myImage.png to be http://www.someurlhere.com this works fine.
How do I load in myImage.png instead? I tried giving it the direct URL:
/home/myUsername/NetBeansProjects/zf2tutorial/module/Album/view/album/album/myImage.png
I've also tried doing the following:
../myImage.png
but neither of these worked.
Also please note, it has to be a bad path (I must be doing something wrong here) because the broken image icon shows up... indicating there is a linking problem here.. the direct URL is copy pasted from the properties section of the image itself..
It's not a problem of image or path. You are trying to make an impossible thing.
You certainly define in your virtualhost configuration that the documentRoot is your public directory.
That means your web server will give access to resources located from this public directory.
You have to put your resources from this public path (css, images..) or you can define a symbolic link if you allow followingSymlinks.
Hope this helps.
<img alt="" src="img/2 header960.png" align="center" >
The fundamental issue you are confusing is the difference between views vs public content.
View files are essentially HTML templates into which you can inject certain values at runtime. Your directory structure correctly places this stuff within a views directory inside your module. These template files themselves are never exposed to the end-user. They are only used by the application to generate fully-populated HTML chunks that are eventually sent to the browser.
In contrast, static assets - like images, javascripts, external stylesheets, etc - are served directly by the webserver to your visitors. As such they need to be in a directory that the webserver has been told to publicly serve to visitors.
The easy thing to do is to simply move directories containing these static assets out of the module and into the app's public folder.
But this is undesirable in the sense that you might reasonably want the module to be a self-contained entity: it can contain its own routes, its own controllers/actions, and, in particular, its own static assets.
So, how to make the webserver reach down into the module and treat some of the content there as "public"?
The answer is to use an "asset manager", a component that links certain module content into the app's public directory. Typically this means:
Activate the asset manager module
Create a public directory inside your module
Place those static assets there
Inform the asset manager which public module directories into the public app directory.
Take a look at the AssetManager module by Roberto Wesley Overdijk and additional instructions for use.
[As a side note, welcome to the world of web development. ZF2 is a great framework, but it's not the easiest thing for a beginner. It provides a lot of flexibility for doing things in a "good way", but it can certainly come off as convoluted at first. Try not to get discouraged. ;-)]
the reason may be the image extension (.png)
please check your exact extension case, is it written in small or capital case???
update your image link exactly as like the name of image.
eg.
myImage.png or myImage.PNG
try to look for an .htaccess file somewhere in your dir most probably on module folder..that file blocks the access for the image you are trying to link..(my basis here is my application in yii which fails to show the image even if the path is correct)
you may create an images folder in your home dir: /zf2tutorial/images for example and put all the images there calling it: src="/images/myImage.png"..
or
find the htaccess im talking about, change the restrictions(or delete it:|) and you can use this path: src="/module/Album/view/album/album/myImage.png"..
<div id='small_image'>
<a href='/feeds/posts/default' title='smallimg'>
<img alt='SMALLIMG' src='c:/home/myUsername/NetBeansProjects/zf2tutorial/module/Album/view/album/album/myImage.png'/>
</a>
</div>

IHttpModule not being applied to virtual directory

I have a network folder that is mapped to my iis app as a virtual directory and I'm trying to do some authentication for files that are located there with an ihttpmodule. I've verified that the ihttpmodule is firing properly for anything else in my app, just not the files located in virtual directory. Most of what I've found is that the directory can't be listed as an application (which it isn't), and everything should work. The other solution that I found was to add the the module tag to the tag, but that didn't seem to help either. Everything that I've found talks about stopping this from happening. So my question is what could be set that is causing this to not work? Is there a certain execute permission that needs to be set? Any other iis settings that could cause this? It is an mvc app, and this is how my directory structure is laid out:
server/app <- my application folder
server/app/content/downloads <- downloads is the virtual directory
Do I have to add the virtual directory directly under my app directory? Is that part of the problem? I don't have direct control of the server my code is running on, so testing things out is a bit of a pain... so I was looking for some more thoughts before starting to send emails off to my operations people. Thanks!
Well, I think I found what the issue is.... it looks like by default, iis6 does not pass executables, zip files, or anything of that nature through the .net runtime. It only does that with .aspx files and such, so it will never fire my ihttpmodule. It looks like you can force it to by adding a mapping for that extension, but is there a code work around for this? Or is that the only way?

Resources