im trying to build my mvc framework and im encountering some problems regarding url.
i have setup my .htaccess file and i can retrieve the url and explode it to an array.
My problem is when i start clicking links on my page, my framework keeps adding them to the url and i end up with a long url that my framework is unable to use to find the right controller.
EX:
at the root of my site the url is:
localhost/root
when i click a link for the first time, the url change to:
localhost/root/controller/model/params
if i click on another link, my url will be:
localhost/root/controller/model/params/controller/model/params <-- here is where i get the problem because the url is not properly formated for the framework to use it.
I dont know if the problem is in the .htacces or in my framework. What i would like to be able to do is regardless of where i am in my webpage i want the url to be always localhost/root
my .htaccess:
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteBase /root
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
and my main php file is:
class main {
protected $controller ="_default";
protected $method ="_getDefaultView";
protected $params;
public function __construct(){
$url = $this->parseUrl();
if(file_exists('app/controllers/'.$url[0].'.php')){
$this->controller = $url[0];
unset($url[0]);
}
require_once('app/controllers/'.$this->controller.'.php');
$this->controller = new $this->controller;
if(method_exists($this->controller, $url[1])){
$this->method = $url[1];
unset($url[1]);
}
$this->params = $url ? array_values($url) : [];
call_user_func([$this->controller, $this->method], $this->params);
}
public function parseUrl(){
if (isset($_GET['url'])){
return $newUrl = explode('/', filter_var(rtrim ($_GET['url'], '/'), FILTER_SANITIZE_URL));
}
}
}
help is appreciated. :-)
My question was answered by #maniteja where he suggest to contruct the links like this
url
all i did was to add /root.
credits are yours #maniteja
Related
My site is created in three languages https://anto-nguyen.com.
It works well to translate from one language to another one.
But.. I can't put the language indication into the URL.
I looked through all questions and answers that I could find here and have tried to use them, but unfortunatelly it doesn't work..
I use Controller LangSwitch
class LangSwitch extends CI_Controller {
public function __construct() {
parent::__construct();
}
function switchLang($language = "") {
$this->session->set_userdata('site_lang', $language);
redirect($_SERVER['HTTP_REFERER']);
}
}
The language is called by following code integrated into menu
<div>
<?= anchor(base_url("langSwitch/switchLang/english"), 'En'); ?>
<?= anchor(base_url("langSwitch/switchLang/french"), 'Fr'); ?>
<?= anchor(base_url("langSwitch/switchLang/russian"), 'Ру'); ?>
</div>
My .htaccess lookes like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /codeigniter/index.php/$1 [L]
And seems to be modified, but all suggestions I found doesn't work
Could you help me what to do that my link will appeared as /mysite/fr /mysite/en /mysite/ru?
All translation are in the folders language/english language/french language/russian
Mayby to change something in routes?
$route['default_controller'] = 'site';
$route['work'] = 'work/index'; //the URL 'work' will redirect to 'work/index'
$route['work/(:any)_(:num)'] = 'work/article/$2';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = 'site/$1';
I'm watching a tutorial video from youtube about making simple route project with just php and I did exactly what he did but there is an error in my project and I can't fix that . when I'm trying to write 'about' or 'contact' the webpage went to object not found! error
Object not found!
The requested URL was not found on this server. If you entered the URL
manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.43 (Win64) OpenSSL/1.1.1g PHP/7.4.5
by the way I'm using xampp
This is my route class
class Route{
private $_uri = array();
/**
* Builds a collection of internal URL's to look for
* #param $uri
*/
public function add($uri){
$this->_uri[] = trim($uri,"/");
}
public function submit(){
$uriGetParam = isset($_GET['uri']) ? $_GET['uri'] : "/";
foreach ($this->_uri as $key => $value){
if (preg_match("#^$value$#",$uriGetParam)){
echo "Match!";
}
}
}
}
This is my php code (index)
include "route.php";
$route = new Route();
$route->add("/");
$route->add("/contact");
$route->add("/about");
$route->submit();
And finally this is my .htaccess file
RewriteEngine On
RewriteBase /route/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
I am trying to figure out the best approach when linking to static pages using a loosely followed MVC design pattern.
I begin by rewriting all requests to the index.php which handles all request and break them down the url into the controller, action and parameters. However if i don't want to follow this url structure and just want to visit a static page such as 'http://example.com/home/' without having to call some action how would i achieve this without getting a php error caused by my router/dispatcher trying to request a file that does not exist?
I thought about setting up some switch statement or a if statement as shown below that checks if the url is set to something then uses a custom defined controller and action, or i wasn't sure whether to take the static resources out of the MVC directory and have it seperate and link to it that way?
<?php
class Router
{
static public function parse($url, $request)
{
$url = trim($url);
if ($url == "/")
{
$request->controller = "tasks";
$request->action = "index";
$request->params = [];
}
else
{
$explode_url = explode('/', $url);
$explode_url = array_slice($explode_url, 2);
$request->controller = $explode_url[0];
$request->action = $explode_url[1];
$request->params = array_slice($explode_url, 2);
}
}
}
?>
This works, but i'd rather not have a huge router setup for many different static resources as it feels tacky and that i am just patching together code. Would putting static pages in its own directory outside of MVC and linking to them in the views be a valid option? i'm relatively new to MVC so any guidance would be great.
Your application shouldn't receive request it is not supposed to handle, you can solve this on a webserver level:
if you are using apache for example, you can setup in the .htaccess file that the request should be directed to your front controller (ex: index.php) only if the requested resource does not exist
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L]
I'm trying to setup a blog script on a website running on the CodeIgniter framework. I want do this without making any major code changes to my existing website's code. I figured that creating a sub domain pointing to another Controller would be the cleanest method of doing this.
The steps that I took to setup my new Blog controller involved:
Creating an A record pointing to my server's ip address.
Adding new rules to CodeIgniter's routes.php file.
Here is what I came up with:
switch ($_SERVER['HTTP_HOST']) {
case 'blog.notedu.mp':
$route['default_controller'] = "blog";
$route['latest'] = "blog/latest";
break;
default:
$route['default_controller'] = "main";
break;
}
This should point blog.notedu.mp and blog.notedu.mp/latest to my blog controller.
Now here is the problem...
Accessing blog.notedu.mp or blog.notedu.mp/index.php/blog/latest works fine, however accessing blog.notedu.mp/latest takes me to a 404 page for some reason...
My .htaccess file looks like this (the default for removing index.php from the url):
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
And my Blog controller contains the following code:
class Blog extends CI_Controller {
public function _remap($method){
echo "_remap function called.\n";
echo "The method called was: ".$method;
}
public function index()
{
$this->load->helper('url');
$this->load->helper('../../global/helpers/base');
$this->load->view('blog');
}
public function latest(){
echo "latest working";
}
}
What am I missing out on or doing wrong here? I've been searching for a solution to this problem for days :(
After 4 days of trial and error, I've finally fixed this issue!
Turns out it was a .htaccess problem and the following rules fixed it:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks to everyone that read or answered this question.
Does blog.domain.co/blog/latest also show a 404?
maybe you could also take a look at the _remap() function for your default controller.
http://ellislab.com/codeigniter/user-guide/general/controllers.html#default
Basically, CodeIgniter uses the second segment of the URI to determine which function in the controller gets called. You to override this behavior through the use of the _remap() function.
Straight from the user guide,
If your controller contains a function named _remap(), it will always
get called regardless of what your URI contains. It overrides the
normal behavior in which the URI determines which function is called,
allowing you to define your own function routing rules.
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}
Hope this helps.
have a "AllowOverride All" in the configuration file of the subdomain in apache?
without it "blog.notedu.mp/index.php/blog/latest" work perfectly, but "blog.notedu.mp/latest" no
$route['latest'] = "index";
means that the URL http://blog.example.com/latest will look for an index() method in an index controller.
You want
$route['latest'] = "blog/latest";
Codeigniter user guide has a clear explanation about routes here
I have installed CodeIgniter_2.1.3 and running in
Windows 7
Wamp Server 2.1
PHP 5.3.5
Apache 2.2.17
In \applicationconfig\routes.php, I created
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['products/catlog'] = "welcome/getOneMethod";
And in \application\controllers\welcome.php, I created a method
public function index()
{
$this->load->view('welcome_message');
}
public function getOneMethod()
{
echo "hi im in newMethod";
}
http://localhost/CodeIgniter_2.1.3/products/catlog
Now I expect on running this url above on browser to give me the page
hi im in newMethod
But instead i'm getting the error message.
Not Found
The requested URL /CodeIgniter_2.1.3/products/catlog was not found on
this server.
What should i do to make it work correctly?
Doo you visit codeigniter documentation website,You can get help from this url how to create static pages
http://ellislab.com/codeigniter/user-guide/tutorial/static_pages.html
For the given url routes and controller.
you will need to use:
http://localhost/CodeIgniter_2.1.3/index.php/products/catlog/
The index.php part in the url can be removed using htaccess rewrite rule.
In the /codeigniter2.1.3/ folder create an .htaccess file and put the following rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /CodeIgniter_2.1.3/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /CodeIgniter_2.1.3/index.php [L]
</IfModule>
Note: This might not be the best way to do it.
EDIT:
Make sure the controller is exactly as bellow,
class Welcome extends CI_Controller {
public function index() {
$this->load->view('welcome_message');
}
public function getOneMethod() {
echo "hi im in newMethod";
}
}
and the routes are as bellow:
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['products/catlog'] = "welcome/getOneMethod";
For the time being remove the htacces, and make it work with index.php in url.
ie:
http://localhost/CodeIgniter_2.1.3/index.php/products/catlog/
Note: since you are able to see the welcome message, there is no problem with your installation or server. there is probably some syntax error. so i recommend you copy paste the above code, as i have checked them and they are working.