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]
Related
I started with a phalcon project and used this tutorial (https://docs.phalcon.io/3.4/en/tutorial-base#basic). But i've got a problem with my controllers.
In my index controller i have:
echo $this->tag->linkTo(
'signup',
'Sign Up Here!'
);
But when i click on Sign Up Here! i get the error message "The requested URL /signup was not found on this server."
I think it has something to do with this part of code, but it looks correct to me.
$di->set(
'url',
function () {
$url = new UrlProvider();
$url->setBaseUri('/');
return $url;
}
);
It's not even showing my exception
$application = new Application($di);
try {
// Handle the request
$response = $application->handle();
$response->send();
} catch (\Exception $e) {
echo 'Exception: ', $e->getMessage();
}
I followed the tutorial on youtube as well and did everything in exact the same way as he did in the video. So i was wondering if anyone can help me out here.
file structure
Thanks
Looks like you are missing the main .htaccess file in the public folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
And this could be the reason why the exception is not being generated.
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
I am new to Laravel, I am working over a small project, using wamp. I was facing problem with pretty URL part, which I figured it out just now.
But now I am getting a weird issue. I have created my application under directory laravel-first-app.
When I am trying to request a page (articles) for example, http://localhost/laravel-first-app/public/index.php/cv, it is showing up the page.
But now if, I request a page for example, http://localhost/laravel-first-app/public/cv, it is saying URL Not Found error.
Below is a brief description what I did.
In my routes.php
Route::get('/', 'WelcomeController#index');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
Route::resource('articles','ArticlesController');
Route::get('articles/delete/{article_id}','ArticlesController#destroy');
Route::get('cv','CvController#index');
Route::get('cv/upload','CvController#upload');
Route::post('cv','CvController#store');
In my CvController
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
class CvController extends Controller {
private $pathToCV;
private $fileName;
public function __construct()
{
$this->pathToCV="cv/";
$this->fileName='piyush_cv.doc';
}
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
return response()->download($this->pathToCV.$this->fileName);
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function upload()
{
return view('cv.upload');
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store(Request $request)
{
dd($request);
if($request->hasFile('cv'))
{
$file = $request->file('cv');
$file->move($this->pathToCV,$this->fileName);
flash()->overlay('File Uploaded','Thanks for uploading the file');
return redirect('cv/upload');
}
flash()->overlay('File was not selected','');
return redirect('cv/upload');
}
}
In .htaccess I have:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Also, for my wamp, I have enabled mod_rewrite module.
Can you please help me out with this? I want to use url http://localhost/laravel-first-app/public/cv instead of http://localhost/laravel-first-app/public/index.php/cv
Please help.
go to the project folder, open terminal and then run php artisan serve it will start localhost:8000 or similar like that then simply go to https://localhost:8000/articles for your required page
I looked carefully to the application structure, I figured out what was the problem.
Now, I resolved the issue and every thing is working fine.
In the Controller, CvController, I had sent a directory CV, and path name was also CV, so it was behaving weirdly.
If you want detailed solution for this, let me know.
Thanks anyways.
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.