Dynamic Routing not properly linking to CSS and JS files - php

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>

Related

How to retain styles within dynamic routes in Laravel 5.4

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

CodeIgniter: My CSS file in my view not working

hallo I am running CodeIgniter 3.0.6 and this is my directory structure:
/application
/assets
/js
/mycss.css
/system
My default controller is loading this file in the index.php just fine but when i load another page, the whole page loads afresh but although this file is getting loaded, it is not doing what i want it to do. When i view sourcepage, the links are OK. What could be the problem. I am using base_url('assets/js/mycss') to load it
To put it simpler i have my default controller as welcome. In the index() method of this controller i have $this->load->view('template/index'). This works fine. Now if i load the same page in another method, uniserve(), in the same controller my css file does not do what i want it to do (my page layout is distorted) but all other css files are doing well. What is the problem here. Kindly help.
This link provide exact problem and exact code but my beyond.min.css is not working after it loads fine. How can i achieve the same solution provided in php's codeigniter? Thank you:
Why can't I load js and css files on my pages?
You could also use link_tag().
<?= link_tag('assets/js/mycss.css') ?>
will give
<link href="http://yoursite.com/assets/js/mycss.css" rel="stylesheet" type="text/css" />
And make sure that base_url is specified in your config.php file

Laravel 5 Views Structure

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') !!}

Laravel #extends failing to pull in master layout

While creating a basic item detail page for a basic blog I've run into a problem with Laravel's #extends not working when pulling in a master layout template. The content from the detail page loads just fine but the master page HTML is not there at all and I'm not getting any errors.
In my routes:
Route::get('blog', 'BlogController#public_index');
Route::get('blog/{id}', 'BlogController#public_single');
In my BlogController:
public function public_single($id) {
$blog = Blog::find($id);
return View::make('public.blog_single') -> with('blog', $blog);
}
At the very top of the blog_single template:
#extends('layouts.public')
All other templates that use this master layout work as expected.
My views directory structure:
views
|
|layouts
| |
| | admin.blade.php
| | public.blade.php
|
|public
|
|blog.blade.php
|blog_single.blade.php
One thing I'm wondering is if the fact that this page looks like it's rendering from a subdirectory is an issue. Here is an example:
This works:
www.mydomain.com/blog
This doesn't:
www.mydomain.com/blog/1
I've looked through the Laravel docs and don't see an answer there. Any ideas? Thanks.
Have you tried to add an absolute path to the master style references?
For example:
<link rel="stylesheet" href="css/bootstrap.min.css">
Has to be:
<link rel="stylesheet" href="/css/bootstrap.min.css">
In all the url's we use. This worked for me :)
Okay, not sure how this fixed it, hopefully someone can give me a little detail.
I found this line of code at the bottom of the blog_single.blade.php file:
<div class="text-center">{{ $blogs->links() }}</p>
That is leftover from a copy/paste of the blog.blade.php file and obviously on a detail page I don't need pagination.
I removed that one line of code and now the templates work as they should. Wondering why Laravel did not throw some sort of error if it was choking on that line, or why that line would effect the whole master file from getting included in the first place.
Anyway, it's fixed.

Understanding CI routing and $page -> can't find .css or .js

The hardest part I'm having with learning Codeigniter is the URI and URL 'theory' if you will.
I followed the original tutorial concerning static pages, and now the code seems to mess EVERY single link up except the main nav bar, requiring me to constantly add routing parameters. I figure I must be doing something wrong.
In my controller, I currently have this code, based off the tutorial:
public function view($page = 'home') {}
My folder structure is:
+ applications
+ views
- welcome.php
+ main
+ css
+ js
- home.php
- about.php
- etc.php
I should point out that the welcome.php page is for the login page. On that page, a link will direct you to home.php (main/home/)
My routing code looks like this:
$route['default_controller'] = 'welcome';
$route['main/(:any)'] = 'main/view/$1';
$route['main/home/home'] = 'main/view/$1';
$route['404_override'] = '';
As you can see, I already had to put a bandaid on it with the <code>$route['main/home/home'] = 'main/view/$1';</code> portion, due to the fact that clicking on "home" while already on the home page would result in linking to main/home/home/ displaying my nav bar, and creating a brand new set of missing links labeled, main/home/about/
In short, I am now trying to reference a .js file and a .css file, but even though the links correctly point to /main/css/style.css it does not recognize it.
Here is my view code for the header (where i load my .css and .js)
<html>
<head>
<title><?php echo $title ?> - TownBuilder - Prototype</title>
<link href="css/structure.css" rel="stylesheet" type="text/css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<header>
<ul>
<li> <?php echo $username ?></li>
<li>Account</li>
</ul>
</header>
Any advice on how to set up routing so that it works correctly?
IMHO they need to remove the route's piece from the codeigniter tutorial because it's not necessary, and it screws everyone up.
Here's the gist of how things work. First comment out all of your routes.
Controller:
Class FirstController extends CI_Controller {
public function home() {
// do stuff
$this->load->view('home');
}
}
Make sure you have a view called home.php in your view folder.
Then the url should be <base_url>/controller/method, so in this case it would be <base_url>/FirstController/home
I think you should add the css/js file on the root directory(outsite of the application folder crete a folder called css) as CI has .htaccess files that deny all access to the application folder. You can only include those files from within your scripts.

Categories