Laravel not loading public/resource files - php

I am using the php server file for local development temporarly. I use localhost:8888 for my laravel application as this:
php -S localhost:8888 server.php
Laravel loads, I can create new Routes etc.. But unfortunatly I seem to be unable to load any CSS files from the public folder.
Since the other similar questions on stackoverflow seem to include .htaccess file, here is mine from the public folder:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
Options +FollowSymLinks
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
And this is my Route:
Route::get('/about', array('as'=>'about',function()
{
return View::make('bootstraped');
}));
This is my 'blade' attempt to load the file:
{{HTML::style('css/main.css');}}
Which is printed as a proper link..
This is my php template attempt (I have also tried putting public before css (no luck)
<link rel="stylesheet" href="<?php echo asset('css/bootstrap-theme.css');?>" type="text/css">
And this is the console output in chrome:
GET http://localhost:8888/css/bootstrap.min.css
about:23 GET http://localhost:8888/css/main.css
about:25 GET http://localhost:8888/css/bootstrap-theme.css
about:27 GET http://localhost:8888/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js
about:95 GET http://localhost:8888/js/vendor/bootstrap.min.js
about:97 GET http://localhost:8888/js/main.js
I have sett correct rights for storage folder and tried 777 on public folder..

Theres a couple of things here. Read this link!
PHP's built in server doesn't use htaccess to my knowledge. Also you are executing server.php from outside your actual webroot. So you will need to need to specify the proper webroot.

Related

How to solve Laravel's Route 404 error on Login page?

A project, which is prepared with Laravel Framework, has handed over to me. Previous developer has not used git, some composer commands and Laravel's .gitignore file(also removed it). On the other hand he has prepared the project on the production server.
Anyway. Firstly I've found a gitignore for laravel and pushed the project to my repository. Then I wanted to pull it to my local. All folders and files pulled except of vendor folder.
Then I executed:
composer install
Gave me this error:
Class 'Maatwebsite\Excel\Excel' not found
Then I fixed it by the method here.
Then retried composer install, gave me this error:
Undefined index: name
Then fixed it by the method here.
Then retried composer install, and succeeded. Then I cleared cache with php artisan cache:clear
I've configured .env file and tried to enter to homepage. It redirected me to login page and gave 404. I've checked the .htaccess, there was redirection like that:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ http://example.com/app/$1 [R=301,L]
# 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>
# php -- BEGIN cPanel-generated handler, do not edit
<IfModule mime_module>
AddType application/x-httpd-ea-php73 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
Changed the example.com to my localhost. But still gives me 404.
Is there anything that I'm missing?
Note: Called the previous developer, and he said me "directly copy and paste the all folders". Hi from 90s :(
OP Said this worked for him so I'm moving it from comments to an answer:
Let's go over what should be there for the pages to return correctly: First you need a routes folder with with all the routes in files inside of it. Second you need the Controller folder (found in /app/Http). Third you need the resources folder with the actual views. Fourth you need the public folder with the CSS/JS, etc.
Since OP stated that all the views work except for the login view:
First find out what the login view page is called, then go to your routes and make sure there's a line such as
Route::get('/login', 'SomeController#somefunction')->name('login.path');
Then go to the controller that is referenced in that route, and make sure that the function returns the view of the login page
Have you tried to generate you laravel application key using php artisan key:generate.
If that also doesn't work then try to remove .htaccess temporary.
Try this command
composer require maatwebsite/excel:^3.0

Laravel remote server error 500, site doesn't works

It's my first upload to a remote server of a Laravel site.
Local I have configured a vhost so my access to my site is like:
site.domain.com //it's pointing htdocs/laravel-proyect/public
I have uploaded my site to my remote server, then:
Change permisions on storage and all its directories
Change permisions to bootstrap
Change configuration of app.php
'url' => 'http://site.domain.com',
Change configuration in database.app with new parameters (as well as in email.php)
Load all tables and data in the data base
Then I try to load my site and get a 500 Internal Server Error
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
It works a little, I have this code in routes.php:
// Begins in the login view
Route::get('/', function()
{
if (!Sentry::check()) {
$data = array();
if (Session::has('email')) {
$data = array('email' => Session::get('email'));
}
return Redirect::route('login', $data);
}
else
return Redirect::route('users');
});
/ ========================================
// LOGIN, LOGOUT AND ACTIVATION SECTION ==========
// // ============================================
// show the login page
Route::get(MyHelpers::textLang('login','routes'), array('as' => 'login', function()
{
if (Sentry::check())return Redirect::route('users');
else {
$rules = User::$rules_login;
// show the login page (app/views/frontend/login.blade.php)
return View::make('frontend.login', compact('rules'));
}
So at first time mo url look like:
site.domain.com/entrar
'entrar', (login in spanish), y set by MyHelpers::textLang('login','routes'), access to my class MyHelpers and to lang files to translate 'login' in to 'entrar', but dont load the template.
Begins to :
Read documentation, making some changes: deleting Multiviews from
.htaccess (deprecated), adding RewriteBase to .htaccess too.
Copy /public content to base dir and change paths in bootstrap/paths and
in the index.php files.
Reinstall in two remote servers to verify is not my provider failing (same error). Talking to my provider, supouse
there are no errors on the server.
I create a new htaccess in the base dir redirecting routes to /public.
Try with php 5.4, 5.5 and 5.6
Actually my .htaccess is:
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]
And I have no more ideas and cant find other solutions as related here.
Any idea? Please, I'm getting crazy, on wensday I have to beguin a new proyect and still working with this one.
Thanks
I have to add
RewriteBase /
to the .htaccess
I have a demo laravel application on heroku Simple Blog.My .htaccess file 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>
#other lines for gzip
It seems that there is no issue with your .htaccess file. It may be problem with you code logic. If you are getting no error with same code on localhost, then check your config again. For laravel 4, there is already a similar issue on stackoverflow Laravel 4 Virtual Host and mod rewrite setup
Note that laravel 5 (if you are using it) takes input (database connection and other setting) from .env file.
If you are still getting error then try to send simple response on index page like:
Route::get('/','basic configurations are correct');
and then make it complex step by step. It will help you in finding error.
Answer from #kikerrobles really works.
your final .htaccess file should look like following
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase /
# 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>

Laravel called from ROOT outsite the /public folder. Getting NotFoundHttpException

I've searched through Web to solve my problem but non of the solutions works for me there, so after couple of hours of struggling with Routing module I've decided to ask you for a helping hand.
Problem
I am unable to access Laravel application from outside /public directory. I need to type localhost\projects\laravel\public in my browser but what I want is to use the URL withour /public. The main reason is because I use shared hosting and have no access to apache configuration file so I'm unable to create vhost.
Background
I've installed manually Laravel framework under the: **c:\xampp\htdocs\projects\laravel** and used composer to do the rest for me.
The routing has been set to: Route::get('/', 'HomeController#showWelcome');
In my project root directory **c:\xampp\htdocs\projects\laravel** I've put a .htacces file with the following content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /projects/laravel/
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
In my Application Root dir c:\xampp\htdocs\projects\laravel\public I have .htaccess with the following code in it:
<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 type: localhost\projects\laravel\ I've got NotFoundHttpException
When I type: localhost\projects\laravel\public I've got welcome page
Any help would be appreciate, Thanks.
Finally I've got this thing working. There are two solutions that can be made.
One requires to move all -public content level-up
Second requires slightly changes in your shared hosting directory root structure - if allowed
Solution #1
The first that works for me that I didn't want to implement has been posted by #Wasim in this thread: Laravel 4 removing public from URL The solution is not save as the content core structure is in the same directory as application itself. This could cause some problems in future implementation.
You need to move all the content from public/ folder one level-up into project ROOT directory then replace internal paths in index.php file for correct onece. For security reasons this .htaccess file needs to be put into project ROOT directory
<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>
Modified Laravel structure described above can be uploaded to your shared web hosting and the application should runs withour /public in yor http address.
Solution 2 (with no access to http.conf file on your shared hosting)
This solution does not requires form you to move the content of the /public folder one level-up but requires form you to have read and write access to ../ROOT directory in your shared hosting (../ROOT directory mostly contains public_html, public_ftp and other folders).
You need to move Laravel scructure into ../ROOT directory as follows:
app/
bootstrap/
vendors/
public_html/
public_ftp/
(...)
Files form /public folder goes to public_html
public_html/index.php
public_hmtl/.htaccess
public_html/packages/
(...)
Then modification for /bootstrap/paths.php is required for line with 'public' key:
'public' => DIR.'/../public' to 'public' => DIR.'/../public_html'
If someone has similar issue and this solution does not work please let me know, thanks.
If you want without public on uri, using composer from your laravel root. Use command php artisan serve and browse localhost:8000 from your browser. It will bring you to homepage.
Where did you get this line from?
RewriteRule ^(.*)$ public/$1 [L]
It's not part of Laravel. Try removing it or the whole .htaccess file in your Laravel root.

access a folder in 'public' directory of laravel

How can I access a folder placed in the public directory of my default laravel installation. This question is for both - testing as well as live purpose.
While Testing-
I am testing on windows using XAMPP's apache http server. I have placed my app's root folder in the htdocs folder of XAMPP. Thus, when I try to access a folder in public directory, 'localhost/myappname/public/blog' it redirects me to 'localhost/blog'.
I don't want this behavior. I want it to pick up the 'index.php' file present at 'localhost/myappname/public/blog/' location. But it doesn't do so.
While Live-
I have my site live on ubuntu with apache http server. When I try to access 'mysitename.com/blog' in the firefox, it gives me this error -
Firefox has detected that the server is redirecting the request for this
address in a way that will never complete.
I have no hint about what's wrong. I have tried spending two days working with laravel's default .htaccess file(in public folder) and apache httpd.conf but still not able to resolve it. Have searched a lot over stackoverflow and google but still no clue about where I am wrong. Please guide me.
Added:
The content of .htaccess file 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]
You just need to comment this line in your .htaccess file.
RewriteRule ^(.*)/$ /$1 [L,R=301]
Comment it or strip out from the file. This will solve both your directory /blog issue as well as redirect loop issue.
I used the Alias feature of apache to overcome this problem. I added an alias for '/blog' in my virtual host configuration.

Laravel Blade directive to properly attach to template a CSS style from 'public' folder

i'm a laravel newbie, and i'm trying to import a css file into my blade template in the views folder.
I have this in the routes file:
Route::get('/', function()
{
return View::make('index');
});
index.blade.php in the 'views' folder has the following code in the header section:
{{HTML::style('css/common.css');}}
</head>
common.css is a file in public folder, inside a css subfolder.
The index.blade.php view runs without errors, showing up all the html elements, but without the css. What am i missing here? Any help would be much appreciated.
Edit: It looks like the problem has to do with some .htaccess or xampp configuration.
The href generated in the page shows [http://localhost/testLaravel/server.php/css/common.css], and that is why it doesn't get the css code. The correct path would be [http://localhost/testLaravel/public/css/common.css].
mod_rewrite is on and my .htacess file 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>
Any help getting rid of the server.php portion of the path would be much appretiated.
try updating your template with the below fragment
{{ HTML::style( asset('css/common.css') ) }}
or manual way
<link href="{{ asset('css/common.css') }}" rel="stylesheet">
The asset() function is a 'shortcut' for the public folder path.

Categories