Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 275 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using CSS in Laravel views?

#1
I've just began learning Laravel, and can do the basics of a controller and routing.

My OS is Mac OS X Lion, and it's on a MAMP server.

My code from routes.php:

Route::get('/', function() {
return View::make('home.index');
});

Route::get('businesses', function() {
return View::make('businesses.index');
});

Route::get('testing', function() {
return View::make('testing.index');
});

Route::get('hello', function() {
return "<h3>Hello world!</H3>";
});


That works, the views display perfectly, ''however'' what I want to try and do is include CSS within the views, I tried adding in a link to a stylesheet within the directory but the page displayed it as the default browser font even though the css was in the HTML!

This is index.php from businesses within the views folder:


<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>

<p>Business is a varied term. My content here.

I tried using the Blade template engine in my other views folder (testing) to display CSS but again the CSS did not show despite it being in the testing folder!

How can I overcome this problem, and get better - as I'm slowly learning this framework.
Reply

#2
your css file belongs into the public folder or a subfolder of it.

f.e if you put your css in

public/css/common.css


you would use

HTML::style('css/common.css');


In your blade view...

Or you could also use the Asset class

[To see links please register here]

...
Reply

#3
You can also write a simple link tag as you normaly would and then on the href attr use:

<link rel="stylesheet" href="<?php echo asset('css/common.css')?>" type="text/css">

of course you need to put your css file under public/css
Reply

#4
That is not possible bro, Laravel assumes everything is in public folder.

So my suggestion is:

1. Go to the **public** folder.
2. Create a folder, preferably named **css**.
3. Create the folder for the respective views to make things organized.
4. Put the respective css inside of those folders, that would be easier for you.

Or

If you really insist to put css inside of views folder, you could try creating *Symlink* (you're mac so it's ok, but for windows, this will only work for Vista, Server 2008 or greater) from your css folder directory to the public folder and you can use `{{HTML::style('your_view_folder/myStyle.css')}}` inside of your view files, here's a code for your convenience, in the code you posted, put these before the `return View::make()`:

$css_file = 'myStyle.css';
$folder_name = 'your_view_folder';
$view_path = app_path() . '/views/' . $folder_name . '/' . $css_file;
$public_css_path = public_path() . '/' . $folder_name;
if(file_exists($public_css_path)) exec('rm -rf ' . $public_css_path);
exec('mkdir ' . $public_css_path);
exec('ln -s ' . $view_path .' ' . $public_css_path . '/' . $css_file);

If you really want to try doing your idea, try this:

<link rel="stylesheet" href="<?php echo app_path() . 'views/your_view_folder/myStyle.css'?>" type="text/css">


But it won't work even if the file directory is correct because Laravel won't do that for security purposes, Laravel loves you that much.
Reply

#5
put your css File in public folder . (public/css/bootstrap-responsive.css)
and `<link href="./css/bootstrap-responsive.css" rel="stylesheet">`
Reply

#6
Copy the css or js file that you want to include, to public folder or you may want to store those in public/css or public/js folders.


Eg. you are using style.css file from css folder in public directory then you can use

` <link rel="stylesheet" href="{{ url() }}./css/style.css" type="text/css"/>`

But in Laravel 5.2 you will have to use

<link rel="stylesheet" href="{{ url('/') }}./css/style.css" type="text/css"/>



and if you want to include javascript files from public/js folder, it's fairly simple too

<script src="{{ url() }}./js/jquery.min.js"></script>
and in Laravel 5.2 you wll have to use

<script src="{{ url('/') }}./js/jquery.min.js"></script>

the function `url()` is a laravel helper function which will generate fully qualified URL to given path.
Reply

#7
Since Laravel 5 the `HTML` class is not included by default anymore.

> If you're using Form or HTML helpers, you will see an error stating class 'Form' not found or class 'Html' not found. The Form and HTML helpers have been deprecated in Laravel 5.0; however, there are community-driven replacements such as those maintained by the Laravel Collective.

You can make use of the following line to include your CSS or JS files:

<link href="{{ URL::asset('css/base.css') }}" rel="stylesheet">
<link href="{{ URL::asset('js/project.js') }}" rel="script">
Reply

#8
Like Ahmad Sharif mentioned, you can link stylesheet over `http`

<link href="{{ asset('/css/style.css') }}" rel="stylesheet">

but if you are using `https` then the request will be blocked and a mixed content error will come, to use it over https use `secure_asset` like

<link href="{{ secure_asset('/css/style.css') }}" rel="stylesheet">

[To see links please register here]

Reply

#9
We can do this by the following way.

<link href="{{ asset('/css/style.css') }}" rel="stylesheet">

{{ HTML::style('css/style.css', array('media' => 'print')) }}


It will search the style file in the public folder of Laravel and then will render it.
Reply

#10
Update for laravel 5.4 ----



All answers have have used the HTML class for laravel but I guess it has been depreciated now in laravel 5.4, so
Put your css and js files in public->css/js
And reference them in your html using

<link href="css/css.common.css" rel="stylesheet" >



Reply



Forum Jump:


Users browsing this thread:
3 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through