laravel 4.1 routing issues - php

im using laravel 4.1 with php 5.3 but the thing is that i my url it only work when i enter
http://localhost/happy_Road/public/index.php/register
i want it to be this way
http://localhost/happy_Road/public/register
because when i add a css file it not getting detected and it show me the route like this
http://localhost/happy_Road/public/index.php/css/bootstrap.css
need some help please :)

You need to follow the instructions for setting up "pretty URLs" at http://laravel.com/docs/installation#pretty-urls. It'll depend on which web server you're using locally - examples are given for both Apache and nginx.
Additionally, you should link to CSS files using absolute URLs - the index.php shouldn't affect your stylesheet routing at all. This can be done in a couple ways:
{{ HTML::style('css/bootstrap.css') }}
or:
<link rel="stylesheet" href="{{ asset('css/bootstrap.css') }}" />

You should use this to add your css files:
{{ HTML::style('css/bootstrap.css') }}
Regarding, index.php, it requires url rewrite and by default in your public folder there is a .htaccess file should be available for apache server. Check Pretty Urls section on Laravel website for more information.

Related

Laravel cannot find stylesheets and nor can I

So I'm very new to Laravel and I'm following a tutorial to use the authentication feature. This works fine, however it is unable to find my scripts and stylesheets. I haven't made any new ones nor have I touched the stylesheet link in app.blade.php, so I have no clue why it is throwing this error.
Code:
<link href="{{ URL::asset('css/app.css') }}" rel="stylesheet">
Error:
GET http://localhost:8000/css/app.css net::ERR_ABORTED 404 (Not Found)
When I go to locahost/css/app.css I get a 404 error. The same error is occurring with app.js. I haven't changed anything about the file structure and it was all generated with composer create-project laravel/laravel
Thanks.
Laravel doesn't include any CSS files by default. You need to create one yourself and put it to /public/ directory, so in your case that would be /public/css/app.css.
Does localhost point to public in your project? Usually you would have to open http://localhost/public
Per default, URL::asset('css/app.css') expects a css file at /public/css/app.css -- for your setup (serving throught php artisan serve, I suppose?), that would be http://localhost:8000/public/css/app.css.
Anyway: If there is no stylesheet, just create one. There is tons of ways how to do it, depending on your needs. Creating one manually for one, but for complex sites think about using laravel mix for compilation. Or anything other way!
Make sure your style exists at public/css directory, then try this code:
<link rel="stylesheet" href="{{asset('css/app.css')}} type="text/css"/>
if the file doesn't exist, create that and try again.
I hope this can help you.

Laravel URLs not working in subfolder

I've finished my laravel project and played it in a subfolder on my domain.
https://example.com/swap
So the above is the root of my directory and when I go there, I do get the index. But now every link I press, I'm redirected to the root of my domain. So I'm getting:
https://example.com/events/1
Instead of https://example.com/swap/events/1
Am I forced to change all URLs by hand in my files are is there a way my htaccess can always redirect to this 'swap' folder and place everything behind that?
I've tried using groups in my routes, without success.
Use url for all your links which are inside views folder:
For subfolder link use:
Sub-Folder Link
For root folder links use this syntax:
Index
There might arise issues with stylesheet and javascript in 'swap" sub-folder links with a 404 error. To fix that, use:
<script type="text/javascript" src="{{asset('js/app.js')}}">
<link rel="stylesheet" type="text/css" href="{{asset('css/app.css')}}">
You need to direct your VHOST to laravel's public folder. This is done for security purposes. Ideally your laravel install is outside the public folder, thus you would have yourdomain.com contain your laravel install and rename your public folder to swap.
IF you really want the name swap in your routes you could use route prefix with groups.
Open .env File and Change Your App Url to https://example.com/swap/ and use routes as links of whole application
All Url's Will Work fine.
When putting a laravel application in a subfolder. You need to modify your .htaccess for subfolder hosting. And add the following:
RewriteBase /swap
This way, the app knows when you go to route {{ url('/hello-world') }}, it will ultimately go to "/swap/hello-world"

Assets not referencing to public folder (Laravel)

I have the public folder inside my laravel project and I have some js and css files inside it.
I'm using the asset function and even though it's referencing to the public folder, my files aren't loaded on the page.
I'm using this code to load (it's only one example, there are more files):
<link href="{{ asset('css/style.css') }}" rel="stylesheet">
And on the browser's console, I'm geting something like this:
Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:8000/css/style.css
Well, I tried to revert the last commit, but no success. Tried to change to URL::asset() function, nothing. Tried everything from the following link: http://laravel.io/forum/09-17-2014-problem-asset-not-point-to-public-folder?page=1 and success.
Please, a little help?
Thanks!
I was having same problem. This is due to moving of .htaccess file from public to root of the project in order to serve localhost/project rather than localhost/project/laravel. And needed to use public as well in the asset:
<link href="{{ asset('public/css/app.css') }}" rel="stylesheet">
Or, modify the asset function from /Illuminate/Foundation/helpers.php
if (! function_exists('asset')) {
/**
* Generate an asset path for the application.
*
* #param string $path
* #param bool $secure
* #return string
*/
function asset($path, $secure = null)
{
return app('url')->asset("public/".$path, $secure);
}
}
The preceding method is not good way. Anyway this would have been easier if there was config for setting asset path.
Add in your env:
ASSET_URL=public
or
ASSET_URL=public/
or
ASSET_URL=/public
which one u need u can do it...
In my case in .env I tried
ASSET_URL=public
with both side slash and alone, nothing helped along with all related SO answers, then opened my old project
and saw in. This worked for me finally (next to clearing views&caches, of course)
ASSET_URL="${APP_URL}"
im talking about localhost side
After you've savely and succesfully setup your .htaccess (like this), your method will work as you would expect it to work:
<link href="{{ asset('css/style.css') }}" rel="stylesheet">
Be aware that all client-side URL requests (requests to images, JavaScript/CSS files, JSON) will be affected by the Rewrite Rules after you've setup your .htaccess.
Also be aware that it might take a while after you see the changes. Cache might interfere with these changes. So, make sure you clear your Laravel cache:
In your Shell run the following commands individually:
php artisan cache:clear
php artisan route:cache
php artisan config:cache
php artisan view:clear
And make sure to disable caching temporarily in your browser settings:
In Google Chrome: Open Dev Tools > Open the Network tab > Check "Disable cache".
Other browsers: search online how to do that.
<link href="{{ asset('/css/style.css') }}" rel="stylesheet">
Try this
Add this is in your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
It Will solve your problem. SURE
Since you have moved your .htaccess file to the root folder you have to change the assets url wherever you are using. The URL should be like below,
<link href="{{ asset('public/css/style.css') }}" rel="stylesheet">
Or, You can just set the ASSET_URL as 'public' in the .env file,
ASSET_URL=http://localhost/projectname/public
sorry for my english, I have same problem in my case working with mike42/escpos-php was because in xampp
I move to zlib.output_compression=on
the correct way is
zlib.output_compression=off
and assets work again
stlye.css is loading and working for me from public folder. Don't forget to add rel="stylesheet"
<link href="{{ asset('/css/style.css') }}" rel="stylesheet">
And make sure to disable caching temporarily in your browser settings:
In Google Chrome:
Open Dev Tools > Open the Network tab > Check "Disable cache".
Other browsers: search online how to do that.
This worked for me. Seems the previous load of CSS and JS is in cache and until it is released the new ones do not load

laravel 4 css files 404 not found

These are my folders:
app
--->views
------->css
----------->font-awesome.css
----------->index.css
------->layouts
----------->restaurants.blade.php
------->restaurants
----------->create.blade.php
in restaurants.blade.php
I tried this:
<link rel="stylesheet" href="{{ URL::asset('views/css/index.css') }}" />
{{ HTML::style('views/css/font-awesome.css') }}
in both way, I got this exception:
http://localhost:8082/ProjectName/public/views/css/index.css 404 (Not Found)
http://localhost:8082/ProjectName/public/views/css/font-awesome.css 404 file not found
Could you help please?
Your assets are stored under your views, but you are linking to public.
Put the assets that you need to load (css, imgs, js) under public. Then you can simply
{{asset('css/style.css')}}
for example.
Also make sure your config/app.php document root is setup right, and if using apache, make sure that your vhost is setup properly to where /public/ is your Document Root.
For using laravel with Vite
#vite("resources/css/app.css")

Symfony Managing Assets directly

Sorry for opening another topic on this but after hours of researching I haven't found a solution:
As stated in symfony's documentation I want to include my assets (css, js, images) directly and without using Assetic. The documentation is recommending the following sniplet:
<script src="{{ asset('js/script.js') }}" type="text/javascript" />
Unfortunately this always ends in a 404 file not found error for every try. I have placed my assets in the myBundle/Resources/public folder and I also copyied it into the web folder using php app/console assets:install web without success.
What am I missing here? Is there a configuration missing? Thank you in advance.
asset('js/script.js') will produce a link like /js/script.js. The files will be copied to a bundles folder, not to the web root. You want something like this to generate the correct URL:
<script src="{{ asset('bundles/myBundle/js/script.js') }}" type="text/javascript"></script>
(Note also that script tags need to have a closing </script> to make them work cross-browser.)

Categories