How to access public folder in laravel 4? - laravel

I was trying to add some styling in my localhost(wamp) lavarel project....
This is my style.css file located in public folder
p{font-weight:bold; size:20px; color:red;}
This is hello.blade.php file located in app/view folder
<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="{{ URL::asset('style.css') }}">
</head>
<body>
<p>Test</p>
</body>
</html>
& My routes.php file is having following code:
Route::get('/',function(){
return View::make('hello');
});
.htaccess file located in root directory is
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
When I am trying to access that hello page by localhost, Test content is visible without any styling. Can anyone tell me what is going wrong with me??

Your directory structure is different from the default one shown here
You have to put the packages folder and these files into the public directory:
.htaccess
favicon.ico
index.php
robots.txt
Then to make your site work again change the document root of your apache server to path/to/application/public
There's a reason for the public directory. When you put it on a server on the internet you gain additional security because the user can only access what is in the public directory. (because the domain is pointing to this folder) If you use your current setup everybody could see your whole application. For example one could enter example.com/app/config/database.php and would see your database password.

Put it like this:
<link rel="stylesheet" href="{{ asset('style.css') }}">
Without URL::

Related

Laravel root directory not public on Hostinger hosting

I currently host my Laravel website on Hostinger, but when I have a image on my page, the url doesn't go to website.com/public/img_url, but it goes to website.com/img_url, does anyone know how this works on Hostinger, and how I can fix this?
Currently my whole project is in my public_html, with no other folders besides it.
The right solution was changing my htaccess to:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
You should need to add a public inside asset function to access a public folder on the shared hosting server.
So you need to change the fetch path of files Example: asset('user/photo.jpg') to asset('public/user/photo.jpg') in each line of code where you need to fetch files from the public directory.

How to solve 404 error after deploying laravel in server under subfolder?

I have uploaded a laravel project in server inside a subfolder under public_html directory.
For example www.xyz.com/subfolder.
Bot I am getting error 404 NOT Found.
My htaccess code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
So, I've been fiddling a lot with VirtualHost(s), but the best solution I found does not involve them at all, and here it is.
First thing to do is to configure .htaccess files for both the app's root folder and its public folder. I found a simple working .htaccess file in this answer, which I report here:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
The default .htaccess works fine, instead, for the public folder.
The last step is to correctly configure your routes in routes.php. The workaround which works for me is quite rough, and maybe one could find some more refined solution, but still here it is.
// Get base (root) route
$base_route = basename(base_path());
Route::get($base_route, function () {
return view('welcome');
});
Route::get($base_route.'/myRoute', function() {
return view('myRoute');
});
So basically you need to prepend $base_route to every route in your app.

Codeigniter deployment problem while hosting to domain server

The project locally works fine $config['base_url'] = "localhost/";
Codeigniter is getting the default controller welcome and login.php, but login.php is not getting the resources from assets folder
all the get requests for a resource are not found. When I inspect I get errors
xGET example.com/assets/TEMPLATE/LoginPage/vendor/bootstrap/css/bootstrap.min.css
net::ERR_ABORTED 404
CodeIgniter folder structure in Cpanel public_html
+application
+assets
+system
index.php
.htaccess
I've configured the application/config/config.php
$config['base_url'] = 'www.mysite.com/';
$config['index_page'] = '';
I've configured the application/config/autoload.php
$autoload['packages'] = array('url');
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
the pages are loading resources:-
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>assets/TEMPLATE/LoginPage/vendor/bootstrap/css/bootstrap.min.css">
Rather than using href="<?php echo base_url();?>assets/TEMPLATE/LoginPage/vendor/bootstrap/css/bootstrap.min.css"> just use href="/assets/TEMPLATE/LoginPage/vendor/bootstrap/css/bootstrap.min.css">
Also, add http:// or https:// to your base URL depending on your SSL status.
Try using link_tag from html helper.
First load html helper in controller, or make it autoload in config/autoload.php
$autoload['helper'] = array('html');
Then try putting this in your header block of view file:
<?php echo link_tag('assets/TEMPLATE/LoginPage/vendor/bootstrap/css/bootstrap.min.css') ?>

Routing fail on Laravel app deployed on subfolder

Ok, this is what is happening.
I've created a small laravel app (actually, still it's on the development stage).
When I upload it to the server (shared hosting managed through cPanel) I've deployed it to a subfolder inside the main website.
There is the main website (a WordPress) on the server's public_html folder, so I created a subfolder where I put my app.
Now, when you request http://oursite.com you reach the Wordpress site.
And when you request http://oursite.com/laravelapp you reach the initial page of the laravel app.
However, when I try to browse any page into the app, the routing returns me to routes inside the root web folder.
For example, if I push the login button, the petition to http://oursite.com/laravelapp/login redirects to http://oursite.com/login, and obviously, the page load fails
The funny thing is that if I reach directly public pages, it seems to work fine.
If I directly request http://oursite.com/laravelapp/public/index.php and push the same login button, a request to http://oursite.com/laravelapp/public/index.php/login is made, and it works!! Even JS and CSS links are correctly detected on that request!!
I suspect this is probably something quite stupid that I cannot see now because I'm too tired, but I will really appreciate any insight on the matter.
The index.php on my laravelapp folder is the following:
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylor#laravel.com>
*/
$uri = urldecode(
parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);
// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
return false;
}
require_once __DIR__.'/public/index.php';
And this is the code of the .htaccess file inside /laravelapp/public folder:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Maybe I would be able to fix this using a subdomain like myLaravelApp.oursite.com?
Ok.
I fixed this problem copying the .htaccess on laravelApp/public to laravelApp.
It seemed that the root folder needed some type of redirection, and this worked like a charm.

Where do javascript and css files go in a laravel project

Where should static javascript and css files in a Laravel 4 application. For the sake of my own understanding, I'm not interested in using any sort of assets or packages yet.
I tried putting them under public/js and public/packages to no avail, getting the following 404 errors in my page:
GET http://localhost/~myuser/mytest/packages/test2.js 404 (Not Found)
GET http://localhost/~myuser/mytest/js/mytest.js 404 (Not Found)
Here is what my .htaccess file looks like for reference:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /~myuser/mytest/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
All assets should be placed in the public folder
public/js
public/css
public/fonts
public/images
You can access the assets using the asset() method or the Laravel HTML helper methods HTML::script and HTML::style.
i.e to include Javascript file:
- <script src="{{ asset('js/yourfile.js') }}"></script>
- {{ HTML::script('js/yourfile.js') }}
Hope this helps!
Inside the laravel folder you got public folder put css and js files here and access like this
<!doctype html>
<html lang="en">
<head>
<title>{{$title}}</title>
{{HTML::style('css/style.css')}}
</head>
<body>
#include('layouts.nav')
#yield('content')
</body>
</html>

Resources