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

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.

Related

Dynamic Routing not properly linking to CSS and JS files

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>

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

Codeignighter linking mishap

Im fairly new to codeignighter and have been trying to make a little Header, content, footer Template.
im currently having a little issue pointing to other views. Heres what i got for code
My head.php
<html>
<head>
<title>TEST HEADER</title>
</head>
My content (main.php)
<body>
Hello World
LINK2
<br><br>
My footer.php
the footer
</body>
</html>
Now the whole deal im having is that i want to link to a inner page (page1.php), i have it setup as a php file in the views folder
page1.php
<?php $this->load->view('head'); ?>
back
<br><br>
<?php $this->load->view('footer'); ?>
I would like to be able to make subpages like this and just inject the header and footer into them. But everytime i try linking to this page1.php from the main, i get a message "The requested URL /CI_Test/views/page1.php was not found on this server." but the file is there?
im a bit confused, any help is appreciated!
Try using <?=site_url('page1.php')?> in main.php:
Hello World
But it's likely that your problem is due to routing and .htaccess mod_rewrite. For CodeIgniter to work, the .htaccess file tells your server to reroute all requests (with a few exceptions depending on setup) to index.php, and CodeIgniter then routes the request to a corresponding controller which renders the view.
Try reading the CI documentation, e.g. about static pages:
http://ellislab.com/codeigniter/user-guide/tutorial/static_pages.html

Code Igniter MVC Displaying Basic View

Excuse me if this is slightly newbie.
I have the main view located # app/views/index.php as:
<?php echo $head ?>
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the User Guide.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
</div>
</body>
</html>
The header_meta.php file located at (app/views):
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<link rel="icon" type="image/png" href="img/favicon.ico" />
<!--meta-->
the controller, named SpecialPage.php located at app/controllers/:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class SpecialPage extends CI_Controller {
function SpecialPage(){
parent::CI_Controller();
}
public function index()
{
$head = $this->load->view('header_meta', '', true);
$this->load->view('index', array('head' => $head));
}
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
Error I'm getting on SpecialPage.php controller:
Call to undefined method CI_Controller::CI_Controller() on line 6
which is:
function SpecialPage(){
parent::CI_Controller();
}
Why is this still just showing a 404 error?????
I suppose it's this line:
<?php $this->load->view('header_meta.php'); ?>
The ".php" extension seems to be the culprit.
BTW, I do not recommend using PHP code in your views (except echoes and loops). Much better is to compose it in your controller:
$head = $this->load->view('header_meta', '', true);
$this->load->view('index', array('head' => $head));
Obviously, the "$t>l>v()" must be changed to "echo $head". Or, the way I prefer (using a template view):
$body = $this->load->view('index', '', true);
$this->load->view('template', array('body' => $body));
Your page should be named as:
specialPage.php instead of index.php under your controller folder.
Read here for more about controller naming conventions.
This is either expected to use like yourhost.com/index.php/specialPage or yourhost.com/specialPage (if your .htaccess rewrite is enabled).
Codeigniter tries to open a file depending upon what classname (via controller/model) you specify. Codenigniter has no idea why it should load index.php for your class files (unless you specify your classname as Index).
And I personally recommend not to use index.php for your files as it may confuse you and others, that this would be a self-loading file. Whereas, in codeigniter, the only self-loading file is index.php in the root folder of your codeigniter installation. And all other files are loaded through index.php (and files that it further includes) only.
The function SpecialPage() should be __construct() and parent::CI_Controller() should be parent::__construct();.
Good luck
Try to add this code to your controller maybe you miss this
function SpecialPage(){
parent::CI_Controller();
}
Note :
Make sure that your controller name is the same with the file name
And I have another question bro,How you call the page to load?
Gudluck!!

How to load a css in CodeIgniter?

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

Categories