Laravel 5 stylesheets and scripts not loading for non-base routes - laravel

I am having trouble linking my stylesheets, scripts, and images for a non-base route in my Laravel 5 app. When I access example.com/about the scripts and stylesheets are working correctly but if I access example.com/about/something the scripts and stylesheets link to example.com/about/css/style.css rather than example.com/css/style.css
My code is as follows:
Routes.php:
Route::get('about/{slug?}', ['as' => 'officerProfile', 'uses' => 'PagesController#about']);
PagesController.php:
public function about($slug = 'president')
{
$officers = Officer::all();
$currentOfficer = Officer::where('slug', $slug)->first();
return view("pages.about", ['title' => $this->makeTitle('Learn About Us')])->with('officers', $officers)->with('currentOfficer', $currentOfficer);
}
layout.blade.php:
{!! HTML::script('https://code.jquery.com/jquery-2.1.4.min.js') !!}
{!! HTML::script('js/skel.min.js') !!}
{!! HTML::script('js/skel-layers.min.js') !!}
{!! HTML::script('js/init.js') !!}
{!! HTML::script('js/scripts.js') !!}
<noscript>
{!! HTML::style('css/skel.css') !!}
{!! HTML::style('css/style.css') !!}
{!! HTML::style('css/style-desktop.css') !!}
</noscript>
For some reason Laravel is thinking the base URL is example.com/about when the loaded route is example.com/about/something. I have tried as well but it still routes to example.com/about/images/image.jpg rather than example.com/images/image.jpg. Any advice would be greatly appreciated. Thank you!

You are using relative paths and you need absolute paths. See the example below, the only thing I added is the prepended /
{!! HTML::script('https://code.jquery.com/jquery-2.1.4.min.js') !!}
{!! HTML::script('/js/skel.min.js') !!}
{!! HTML::script('/js/skel-layers.min.js') !!}
{!! HTML::script('/js/init.js') !!}
{!! HTML::script('/js/scripts.js') !!}
<noscript>
{!! HTML::style('/css/skel.css') !!}
{!! HTML::style('/css/style.css') !!}
{!! HTML::style('/css/style-desktop.css') !!}
</noscript>
A relative path loads files related to the current path. Eg from the page http://example.com/about/us you can relatively load images/logo.png which results in the actual request to http://example.com/about/images/logo.png.
An absolute path is always from the hostname: same example as above but you'd load /images/logo.png which results in http://example.com/images/logo.png

Related

Laravel Stop SSL Redirect on FORM Helper

I have a web form in Laravel that is behind an ip. Example:
127.0.0.1/posts/create
With a simple form helper:
{{ Form::open(array('url' => '/posts','files'=>'true')) }}
#include('posts._form')
{!! Form::submit('Create Post', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
When I inspect the html, https is added to the url. How can I stop this?
Found the problem, my nginx had the HTTPS variable set on. Laravel apparently reads that variables and adds the protocol + host_name to all redirect actions.

use hash tag in end of form url?

i want to submit something like this
<form action="submit#myresults">
I have read here:
https://laravel.io/forum/02-07-2014-how-to-append-hashtag-to-end-of-url-with-redirect
so this won't work:
{!! Form::open(['route' => 'my_fooroute']) !!}
what is the cleanest way?
this seems to be the cleanest form:
{!! Form::open(['url' => route('my_fooroute') . '#bar'] !!}
You can do something like:
Route::get('/blog#{hash_tag?}', 'Controller#Method')->name('test');
Then, you open form like:
{!! Form::open(['route' => ['test', 'hash_you_want']]) !!}
Or, URL like:
My Link

How can I include html within a form label using Laravel Collective?

Reading through this SO thread I have read that I can create a new macro to create custom form inputs.
I am brand new to Laravel development (big surprise) & it seems a tad overkill for such a small thing. Is there a "simpler" way to have something like this:
blade template
{!!Form::label('firstName', 'First Name<sup>*</sup>') !!}
{!! Form::text('firstName', null, ['class'=>'required']) !!}
html
<label for="firstName">First Name*</label>
<input type="text" name="firstName" class="required">
Or, is this a case where I just write the html, and then use the form service to create the inputs?
Thank you for your patience and insight.
The simple way to do this is
{!! Form::label('labelFor','labelText',[],false) !!}
the last parameter is $escape_html which default value is "true".
The Form class attributes will always be escaped (they were in Laravel 4 and still are with Laravel Collective's 5+ support), therefore no HTML is allowed. Since you're needs are so simple, I'd suggest just writing plain HTML.
If you want overkill, maybe something like this in your AppServiceProvider.php:
Form::macro('labelWithHTML', function ($name, $html) {
return '<label for="'.$name.'">'.$html.'</label>';
});
Then, in your Blade templates:
{!! Form::labelWithHTML('firstName', 'First Name<sup>*</sup>') !!}
{!! Form::text('firstName', null, ['class'=>'required']) !!}
{!! Html::decode(Form::label('email', 'E-Mail Address', ['class' => 'text-muted'])) !!}
that is much better to fix these type of problems
Maybe it's late to answer, but you could do this:
{!! Html::decode(Form::label('firstName','FirstName: <sup>*</sup>')) !!}
{!! Form::text('firstName', null, ['class'=>'required']) !!}

Laravel use an icon to delete content

I have the following delete button:
{!! Form::open(['route' => ['tasks.destroy', $type->id], 'method' => 'delete']) !!}
{!! Form::submit('Erase this task?', ['class' => 'delete']) !!}
{!!Form::close() !!}
And now I would like to not use a button, but an icon to delete content. I would like to use this button: http://fortawesome.github.io/Font-Awesome/3.2.1/icon/remove-sign/
<i class="icon-remove-sign"></i> icon-remove-sign
The sign should be blue and small.
How do I do that? I only get error messages relating to models as a result. But when I dont make any changes it works perfectly.
Can someone give me an example of how to make the picture point to a delete route?
The form helper does not allow you to add HTML to it. Use a simple HTML button:
{!! Form::open(['route' => ['tasks.destroy', $type->id], 'method' => 'delete']) !!}
<button class="delete">
<i class="icon-remove-sign"></i> Erase this task
</button>
{!!Form::close() !!}

Laravel white page loading route

I had a login.blade.php with some code, and I decided to replace with a new one.
I renamed it to old_login.blade.php and create a new file, in the same path, with the name login.blade.php.
After a while, I decided to rollback to my old login page.
I delete login.blade.php, and renamed the old_login.blad.php with the original name to return back.
No code was edited, only views.
The problem is that the page returned a blanck white page with many comments (the comment's tag is not closed).
I try to make a copy of the view, called test.blade.php, and change the route to that view. It diplay them correctly. But If I change another time route to myapp.login to display login.blade.php view, it won't work.
I try anything but nothing is changed. I'm using laravel 5.1.
The code insiede my routes.php is
Route::get('/', function () {
return view('myapp.login');
});
the code inside the login.blade.php (same ad test.blade.php) is:
#extends("app")
#section("content")
{!! Form::open(["url" => "home"]) !!}
<div class="form-group">
{!!Form::label("username", "Username:") !!}
{!!Form::text("usr_username", "Luca", ["class" => "form-control"]) !!}
</div>
<div class="form-group">
{!!Form::label("password", "Password:") !!}
{!!Form::input("password", "usr_password", "mypass", ["class" => "form-control"]) !!}
</div>
<div class="form-group">
{!!Form::submit("Login", ["class" => "btn-primary form-control"]) !!}
</div>
{!! Form::close() !!}
#include("errors.list")
#stop
So a nonsense answer to resolve my problem: I cutted the code inside login.blade.php, save file, pasted it, and saved another time. Now it display correctly.

Resources