.css file doesn't get carried out - php

Style.css doesn't get carried out when url is http://example.com/mvc/login/requestsExceeded,
but it does in my login index page that is http://example.com/mvc/login,
if I add forward slash http://example.com/mvc/login/, then it doesn't work either.
mvc = site in subdirectory
login = controller
requestsExceeded = view
.css file is in http://www.example.com/mvc/views/themes/default/style.css
The file path is ok becouse i've tryed like this:
<?php if(file_exists("views/themes/{$theme}/style.css")) echo 'TEST'; ?>
<link href="views/themes/<?=$theme;?>/style.css" type="text/css" rel="stylesheet" />
and it does echo out TEST.
Here my simplified router:
<?php
$controller = "Index";
$action = "index";
$query = null;
if (isset($_GET['load']))
{
$params = array();
$params = explode("/", $_GET['load']);
$controller = ucwords($params[0]);
if (isset($params[1]) && !empty($params[1]))
{
$action = $params[1];
}
if (isset($params[2]) && !empty($params[2]))
{
$query = $params[2];
}
}
$modelName = $controller;
$controller .= 'Controller';
$load = new $controller($modelName, $action);
if (method_exists($load, $action))
{
$load->{$action}($query);
}
else
{
die('Invalid method. Please check the URL.');
}
I'm pretty sure that this is caused by .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?load=$1 [PT,L]
-I would like to restrict all access unless index.php...,
-Allow access to .css,.gz,.js and image files,
-remove the forward slash from url,
-redirect 301 to index.php,
-redirect http://example.com/mvc/home to index.php,
Help would be appreciated!

Change your import to
<link href="/mvc/views/themes/<?=$theme;?>/style.css" type="text/css" rel="stylesheet" />
If you want the same link to work from anywhere in your site, then you'd better let the path be absolute, that is start with /.

Related

.htaccess doesn't work with CSS files

I have a .htaccess file with these rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I also have a Router.php file:
<?php
class Router
{
function __construct()
{
print_r($_GET);
$this->request = $_GET['url'];
$this->request = rtrim($this->request, "/");
$this->params = explode("/", $this->request);
print_r($this->params);
$this->controller = $this->params[0];
if ($this->controller == "index.php")
$this->controller = "Index";
$this->controller = ucfirst($this->controller);
$file = 'controllers/' . $this->controller . '.php';
if (file_exists($file)) {
require_once $file;
$this->connection = new $this->controller($this->params);
} else {
$file = 'controllers/PageNotFound.php';
$this->controller = "PageNotFound";
require_once $file;
$this->connection = new $this->controller();
}
}
}
and header.php
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="utf-8">
<link href="resources/style.css" rel="stylesheet" type="text/css">
<title>System stypendialny</title>
</head>
<body>
I have a problem with the .htaccess file. When I use this version of the file and I try this http://localhost/scholarship_system/ URL in the browser I see this:
Array ( )
Notice: Undefined index: url in C:\xampp\htdocs\scholarship_system\libs\Router.php on line 8
Array ( [0] => )
But when I remove this line (RewriteCond %{REQUEST_FILENAME} !-f) then the CSS file is not loaded.
You can keep your .htaccess as it is. If you remove -f condition, you're router will need to handle all requests to css, images and javascript-files as well and that's just a pain.
Set a default controller in your Router-class instead:
$this->request = isset($_GET['url'])? $_GET['url] : 'default';
then you just need to create the file controllers/default.php which will be used if the $_GET['url] isn't set.

Php mvc not loading images using <img>

I have made my own php MVC and it works perfectly for everything except when it comes to loading certain images. For example it will load like icons, and some other images but when it comes to using the tag it wont load the image but when i look in the google chrome developer inspector tool it shows that my tag images aren't loading. Its giving back a 301 number and loading the HTML. So in terms its like my MVC is thinking the URL for the image is a page!I want my MVC to only make requests to my index.php file which is in the root directory for my site. Also when it comes to it generating the HTML code i mean that it going to my error html page when i put the direct image url
in the browser.
Absolute URL for the image:
http://localhost/website/user_data/cfedabd2e283c19c06f3b8b5bd45df4bb66c2b3a/photo.jpg
Here is my code below:
.htaccess--
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?u=$1 [PT,L]
</IfModule>
bootstrap.php(Router)--
class Bootstrap {
function __construct() {
$url = $_GET['u'];
$url = rtrim($url, '/');
$url = explode('/', $url);
if (empty($url[0])) {
require 'controllers/index.php';
$controller = new Index();
$controller->index();
return false;
}
$file = 'controllers/' . $url[0] . '.php';
if (file_exists($file)) {
require $file;
} else {
$this->error();
}
$controller = new $url[0];
$controller->loadModel($url[0]);
// calling methods
if (isset($url[2])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}($url[2]);
} else {
$this->error();
}
} else {
if (isset($url[1])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}();
} else {
$this->error();
}
} else {
$controller->index();
}
}
}
function error() {
header('location: ' . APP_URL . 'index');
}
index.php(Where i want all the requests to go)--
<?php
//error_reporting(E_ALL);
session_start();
/* Require all config files */
require 'config/config.php';
/* Initiate system */
$bootstrap = new Bootstrap;
?>
Example What the user sees--
<h3>Error page not found</h3>
The image with
$photo = "http://localhost/website/user_data/cfedabd2e283c19c06f3b8b5bd45df4bb66c2b3a/photo.jpg;
<img src="<?php echo $photo; ?>" width="200" style="max-height: 200;"/>

How do I get clean URLS for only 2 variables?

I've seen/read questions about clean URLs with .htaccess, but for the life of me, I cannot get them to work for my specific needs. I keep getting 404 message.
Example: www.mysite.com/article.php?id=1&title=my-blog-title
I would like for url to be: www.mysite.com/article/1/my-blog-title
Here's what I have so far in my .htaccess:
Options -MultiViews
#DirectorySlash on
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [L]
# Rewrite for article.php?id=1&title=Title-Goes-Here
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+) article.php?id=$1&title=$2 [NC,L]
#Rewrite for certain files with .php extension
RewriteRule ^contact$ contact.php
RewriteRule ^blogs$ blogs.php
RewriteRule ^privacy-policy$ privacy-policy.php
RewriteRule ^terms-of-service$ terms-of-service.php
Also, is this how I would link to article? article.php?id=<?php echo $row_rsBlogs['id']; ?>&slug=<?php echo $row_rsBlogs['slug']; ?> or article/<?php echo $row_rsBlogs['id']; ?>/<?php echo $row_rsBlogs['slug']; ?>
I'm using Dreamweaver, but I am comfortable hand coding.
Thanks in advance.
You could use a dispatcher by telling the webserver to redirect all request to e.g. index.php..
In there a dispatch instance analizes the request and invokes certain controllers (e.g. articlesControllers)
class Dispatcher
{
// dispatch request to the appropriate controllers/method
public static function dispatch()
{
$url = explode('/', trim($_SERVER['REQUEST_URI'], '/'), 4);
/*
* If we are using apache module 'mod_rewrite' - shifting that 'request_uri'-array would be a bad idea :3
*/
//array_shift($url);
// get controllers name
$controller = !empty($url[0]) ? $url[0] . 'Controller' : 'indexController';
// get method name of controllers
$method = !empty($url[1]) ? $url[1] : 'index';
// get argument passed in to the method
$parameters = array();
if (!empty($url[2])) {
$arguments = explode('/', $url[2]);
foreach ($arguments as $argument) {
$keyValue = explode('=',$argument);
$parameters[$keyValue[0]] = $keyValue[1];
}
}
// create controllers instance and call the specified method
$cont = new $controller;
if(!method_exists($cont,$method)) {
throw new MethodNotFoundException("requested method \"". $method . "\" not found in controller \"" . $controller . "\"");
}
$cont->$method($parameters);
}
}
in .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php

How to prevent 403, 400 errors URL forwarding?

Site works good when user enter any url which doesn't exist and forward request to error controller.
But
If you write a script in url site throw 403 error
If you write some asp codes site throw 400 error
How can i prevent this and forward them to custom 403 and 404 pages? I tried forward with htaccess but i couldn't succeed it.
Another problem:
If you write ANY ascii code in adress bar, bootstrap forward to welcome page (controller ->index.php) .How is possible?
Directory structures, htaccess and bootstrap codes are below. Thank you for any help.
Directory structure:
/config
/libs
-bootstrap.php
/controllers
/models
/views
/public_html
-index.php
htaccess
htaccess
htaccess in main directory
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public_html/ [L]
RewriteRule (.*) public_html/$1 [L]
</IfModule>
htaccess in public_html directory
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
index.php in public_html
<?php
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', dirname(dirname(__FILE__)));
require_once (ROOT . DS . 'libs' . DS . 'bootstrap.php');
$app = new Bootstrap();
bootstrap.php in libs
<?php
class Bootstrap {
function __construct() {
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = explode('/', $url);
print_r($url);
if (empty($url[0])) {
require '../controllers/index.php';
$controller = new Index();
$controller->index();
return false;
}
$file = '../controllers/' . $url[0] . '.php';
if (file_exists($file)) {
require $file;
} else {
$this->error();
return false;
}
$controller = new $url[0];
// calling methods
if (isset($url[2])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}($url[2]);
} else {
$this->error();
}
} else {
if (isset($url[1])) {
if (method_exists($controller, $url[1])) {
$controller->{$url[1]}();
} else {
$this->error();
}
} else {
$controller->index();
}
}
}
function error() {
require '../controllers/error.php';
$controller = new Error();
$controller->index();
return false;
}
}

Themes outside application

I read this post and I want to use similar solution, but with db.
In my site controller after():
$theme = $page->get_theme_name(); //Orange
Kohana::set_module_path('themes', Kohana::get_module_path('themes').'/'.$theme);
$this->template = View::factory('layout')
I checked with firebug:
fire::log(Kohana::get_module_path('themes')); // D:\tools\xampp\htdocs\kohana\themes/Orange
I am sure that path exists. I have directly in 'Orange' folder 'views' folder with layout.php file.
But I am getting: The requested view layout could not be found
Extended Kohana_Core is just:
public static function get_module_path($module_key) {
return self::$_modules[$module_key];
}
public static function set_module_path($module_key, $path) {
self::$_modules[$module_key] = $path;
}
Could anybody help me with solving that issue?
Maybe it is a .htaccess problem:
# Turn on URL rewriting
RewriteEngine On
# Put your installation directory here:
# If your URL is www.example.com/kohana/, use /kohana/
# If your URL is www.example.com/, use /
RewriteBase /kohana/
# Protect application and system files from being viewed
RewriteCond $1 ^(application|system|modules)
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ / [PT,L]
RewriteRule ^(media) - [PT,L]
RewriteRule ^(themes) - [PT,L]
# Allow these directories and files to be displayed directly:
# - index.php (DO NOT FORGET THIS!)
# - robots.txt
# - favicon.ico
# - Any file inside of the images/, js/, or css/ directories
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|static)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
Could somebody help?
What I am doing wrong?
Regards
[EDIT]
My controller code:
class Controller_Site extends Controller_Fly {
public static $meta_names = array('keywords', 'descriptions', 'author');
public function action_main() {
$this->m('page')->get_main_page();
}
public function action_page($page_title) {
$this->m('page')->get_by_link($page_title);
}
public function after() {
$page = $this->m('page');
$metas = '';
foreach(self::$meta_names as $meta) {
if (! empty($page->$meta)) {
$metas .= html::meta($page->$meta, $meta).PHP_EOL;
}
}
$theme = $page->get_theme_name();
Kohana::set_module_path('themes', Kohana::get_module_path('themes').'/'.$theme);
$menus = $page->get_menus();
$this->template = View::factory('layout')
->set('theme', $theme)
->set('metas', $metas)
->set('menus', $menus['content'])
->set('sections', $page->get_sections())
->set_global('page', $page);
if ($page->header_on) {
$settings = $this->m('setting');
$this->template->header = View::factory('/header')
->set('title', $settings->title)
->set('subtitle', $settings->subtitle)
->set('menus', $menus['header']);
}
if ($page->sidebar_on) {
$this->template->sidebar = View::factory('sidebar', array('menus' => $menus['sidebar']));
}
if ($page->footer_on) {
$this->template->footer = View::factory('footer');
}
parent::after();
}
}
and parent controller:
abstract class Controller_Fly extends Controller_Template {
protected function m($model_name, $id = NULL) {
if (! isset($this->$model_name)) {
$this->$model_name = ORM::factory($model_name, $id);
}
return $this->$model_name;
}
protected function mf($model_name, $id = NULL) {
return ORM::factory($model_name, $id);
}
}
[Edit 2]
Previous link to post was dead, link was:
http://forum.kohanaframework.org/comments.php?DiscussionID=5744&page=1#Item_0
My guess is that your controller has a __construct() method and you aren't calling parent::__construct
Actually, I think for kohana V3 __construct needs to also be passed the $request object as follows:
public function __construct(Request $request)
{
parent::__construct($request);
}
I realized that I need to init all modules once again:
$theme = $page->get_theme_name();
Kohana::set_module_path('themes', Kohana::get_module_path('themes').'/'.$theme);
Kohana::modules(Kohana::get_modules());
Now I don't have an error. Instead I am getting white screen of death:
$this->template is null
:(
[EDIT]
Now I know, I had:
$this->template = View::factory('layout')
->set('theme', $theme)
->set('metas', $metas)
->set('menus', $menus['content'])
->set('sections', $page->get_sections())
->set_gobal('page', $page);
Ofcourse I forgot that set_global doesn't return $this :D
Anyway everything working now :)
I have one more question about efficiency. I am calling Kohana::modules() second time in request flow. Is this a big deal according to efficiency?
Regards

Categories