In the application I built I have a view called main (http://127.0.0.1:8000/main). Following is how I load the view file from Route file, web.php.
Route::resources(['/main' => 'pages\MainController']);
Above loads the 'main.blade.php' view controller from the index function of MainController.php file.
public function index() {
return view('pages\'main');
}
My problem is, I have an edit button in the view which the href value is set as following.
<a href="main/{{$data->id}}/edit" >Edit</a>
Click of the above will direct me to 'http://127.0.0.1:8000/main/1/edit' route
and this route will not load any styles and javascript files like it was loaded in 'http://127.0.0.1:8000/main' . What is the work around to retain the resource styles and js functionality ?
Following is one example I have linked my resource css in my main layout blade
<link href="vendors/font-awesome/css/font-awesome.min.css" rel="stylesheet
You need to include the resource URLs within url php function.
{{url('vendors/font-awesome/css/font-awesome.min.css')}}
So the complete code is.
<link href="{{url('vendors/font-awesome/css/font-awesome.min.css')}}" rel="stylesheet">
href="{{ URL :: asset ( ' your css path ') }}"
Make sure you css is in public directory
Related
I'm loading bootstrap this way on my view page :
<link rel="stylesheet" href="<? echo base_url('assets/css/bootstrap.min.css')?>">
autoload: $autoload['helper'] = array('url');
configs: $config['base_url'] = 'http://localhost/helloby/';
Below is the path of the controller:
http://localhost/helloby/index.php/login/home
But when I copy the same code at welcome_message view bootstrap loads normally. I don't know why it's happening.
Try using site_url() instead of base_url()
base_url returns the url without index.php
Check if you are loading the bootstrap css in every view, you cant just load it in one view and expect that all other views know about it.
Common way of doing things like this is to create header and footer files that are shared between all views.
I solved the problem, I was reading an e-book of best practice of Codeigniter and the call of bootstrap was a little bit different.
This way : href="<?=base_url('/assets/css/bootstrap.min.css')?>
and working fine!
I'm currently trying to set up a project in Fat-Free-Framework 3.6 - but I dislike the idea of having to manually add all my basic routes to routes.ini, and I decided I wanted to try my hand at more dynamic routes.
For this, I've made 2 new GET routes. Here is what I have:
$f3->route('GET /#module','{#module}_controller->index');
$f3->route('GET /#module/#method','{#module}_controller->#method');
The first one is to load a controller and it's default method (Which I have made index in these examples).
The first route works great, if I navigate my browser to http://example.com/ex1, it will load controller ex1_controller and run the index method, and everything will run and display properly.
The second route however, does not work as expected. When trying to run the exact same method within the second route, I get no CSS. For example, if I now navigate to http://example.com/ex1/index, there is no CSS on the page - but when I inspect the page source, I see the CSS should be loaded in as <link rel="stylesheet" type="text/css" href="assets/css/uikit.min.css"> is in the page.
Upon further investigation, I followed the link to the CSS file in my source code which brought me to the incorrect link. During the 2nd route, for some reason it's not trying to load from the root. It's trying to load from the URL ./ex1/assets/css/uikit.min.css, but the CSS actually exists in ./assets/css/uikit.min.css
Structure:
index.php - Where routes are defined
| app
| controllers
Controller.php
ex1_controller.php
| models
ex1.php
| views
| ex1
index.htm
| assets
| css
uikit.min.css
| js
uikit.min.js
ex1_controller.php
<?php
class ex1_controller extends Controller {
function index() {
$example = new ex1($this->db);
$output = $example->returnOutput();
$this->f3->set('output', $output);
echo $this->template->render('ex1/index.htm');
}
}
The Controller.php file is run on every route, which is then extended by the controller for the module. This is where the main_header.htm file is loaded in, which contains the css/js links. This is how the links are defined in main_header.htm;
<link rel="stylesheet" type="text/css" href="assets/css/uikit.min.css">
<script src="assets/js/uikit.min.js"></script>
<script src="assets/js/uikit-icons.min.js"></script>
Why is F3 trying to load CSS/JavaScript from the wrong directory when using dynamic routes such as this, and how can I fix it?
The fix for this seems to be quite easy.
Simply add a slash to the beginning of the linked resource path.
<link rel="stylesheet" type="text/css" href="/assets/css/uikit.min.css">
<script src="/assets/js/uikit.min.js"></script>
<script src="/assets/js/uikit-icons.min.js"></script>
I am currently working on a new project, and for the first time I am using Laravel. I've created several blade files (views) and added the routes of the views. Because there are so many pages I would like to have my Navigation content in one file, called a base file. I extend the base.blade.php file in every view so my navigation is everywhere the same. But, I have now the problem that some links are not valid, and have no route (because they are pointing from different locations). What is the best way to link to other blade files (and views) in one single file? What I have now:
my route
Route::get('/jaarhoroscoop', function () {
return view('jaarhoroscoop');
});
my nav file
<li>
<a class="headnavitem" href="jaarhoroscoop">Jaarhoroscoop</a>
</li>
So when I am on the index file, it will link me to the view of "jaarhoroscope". But when I am on a page of a different view and in a other directory..
Route::get('/chinese-horoscopen/jaarhoroscoop-2016-2017', function () {
return view('chinese-horoscopen.jaarhoroscoop-2016-2017');
});
Like the view above, it will take me to public/chinese-horoscopen/jaarhoroscoop. I cant get it right.. Who can help me out? What I would like is that if I am on above route, I would go to public/jaarhoroscoop instead of public/chinese-horoscopen/jaarhoroscoop.
Thanks!
If i understood your problem. When you are in (root) / and click as an example 'categories' the roote becomes /categories.
When you are in /categories and click an other navigation link it appends it to the /categories and not in the root.
The solution i give is:
1. name your routes for easier use across the site
Route::get('/jaarhoroscoop', [
'as' => 'jaarhoroscoop.show',
'uses' => 'JaarhoroscoopController#index'
]);
2. In your view or your nav view make the links like:
Jaarhoroscoop
I have added a parameter but you can avoid it in your case.
Edited:
If you don't want to use a controller:
Route::get('/jaarhoroscoop', function () {
return view('jaarhoroscoop');
})->name('jaarhoroscoop.show');
Laravel also has a helper function called url which can generate absolute urls instead of using relative urls which tend to create problems like the one faced by you. All you need to do is this (assuming you are using blade for nav file as well)
<a class="headnavitem" href="{{ url('jaarhoroscoop') }}">Jaarhoroscoop</a>
else for vanilla php file you can do:
<a class="headnavitem" href="<?php echo url('jaarhoroscoop') ?>">Jaarhoroscoop</a>
This will always generate the output:
<a class="headnavitem" href="your_website.com/jaarhoroscoop">Jaarhoroscoop</a>
I added a bootstrap template to laravel 5 Resources directory.
And added only the index.php to views in Resources.
The index.php file is pointing the files like :
<link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
And its not loading.
In my routes.php i am loading :
Route::get('/', function () {
return view('index');
});
As I can see in the code, ViewFinder does a case sensitive search for a template based on whatever you provide to the view() helper.
Pass Index to view() instead of index or change the template name from Index.php to index.php.
If your view renders correctly, but CSS file is not loaded, it means it's inaccessible to the browser. Files that are not returned via Laravel need to end up somewhere in /public folder so that web server can read them and return to the browser. You need to place them there manually or using some build toos (gulp, grunt, ...).
In your case, your bootstrap file must end up in public/bootstrap/css/ folder.
You could also fetch the bootstrap file from a CDN by using one of absolute URLs you can find here: http://www.bootstrapcdn.com/, e.g.:
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
Add bootstrap files inside the public folder. Suppose your bootstrap file inside the laravel\public\css then:
{!! Html::style('css/style.css') !!}
and jquery files laravel\public\js
{!! Html::script('js/jquery.js') !!}
I'm new to programming. I want to fetch the css saved in DB and load in a php file. I'm using CodeIgniter. After loading the css, I want to link to it as an external css. I tried following, but it is not working.
EDIT:
defaultCss.php is the file in which I want to load the css.
$strTemplate.="<link rel='stylesheet' type='text/css' href='".base_url()."user/defaultCss.php'>"
While, I view the page source it gives "Page not found" error.
Below are my controller and view code.
function loadDefaultCSS(){
$this->load->model('UserModel');
$this->UserModel->loadDefaultCSS();
}
View :
if(isset($strTemplateStyles) && $strTemplateStyles!="") {
echo $strTemplateStyles;
}
Model function :
function loadDefaultCSS($strTemplateStyles){
$data['strTemplateStyles']=$strTemplateStyles;
}
Why this is not working ? what is the issue ?
You can use template library for codeigniter.
It provides more flexibility for handling views, loading js and css files.
Also it provides an option for splitting the views into sections like header, content, footer etc. The template library link i have provided above is easy to use and integrate.
It also has very good documentation.
In the above template library the css and js files can be loaded as follows (write below code in controller) -
For loading css files -
$this->template->add_css('path to css file');
For loading js files -
$this->template->add_js('path to js file');
For detailed documentation you can refer above hyperlink.
Well the name of your controller action is loadDefaultCSS, so I would expect the URL for the generated stylesheet to be: (assuming your controller is indeed called User)
base_url()."user/loadDefaultCSS"
Does this work?:
$strTemplate .= '<link rel="stylesheet" type="text/css"
href="'.base_url().'"user/loadDefaultCSS">';
I can see a few strange things in your code:
You should not use .php in your CI URLS
How can your view possibly get the style of the user when you're not passing it to the view from your controller?
How do you know what user it concerns? I assume you have not posted all your code?
What happens if you actually open the stylesheet URL that you generate? Does it throw a 404? A CI error?
The Best way to load css is to pass css paths from controller to view and render it from view in large application we its not good practice to load everything in header it can cause performance issue
login_view.php / view
css code
<?php
if(!empty($css_files)){
foreach ($css_files as $css_path) {
?>
<link rel="stylesheet" href="<?php echo $css_path;?>">
<?php
}
}
?>
login.php /controller
$data['css_files'] = array(
base_url('assets/bootstrap/css/bootstrap.min.cs'),
base_url('assets/plugins/iChecksquare/blue.css'),
'https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css');
$this->load->view('login',$data);
same technique you can use for javascript libs
note that sequence of the files are sensitive