I'm learning Slim Framework and got stuck with its routing.
Working Code. Code snippet #1:
$app = new \Slim\App();
$app->get("/", function () {
echo "Hello SlimFramework";
});
$app->run();
Not Working. Code snippet #2:
$app = new \Slim\App();
$app->get("/hello/{name}", function (Request $request, Response $response) {
$name = $request->getAttribute('name');
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
I'm getting "Not Found
The requested URL /hello/name was not found on this server." for Code snippet #2. Any clue what's going on here ?
.htaccess File
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
Thanks in advance !
I had to start with clean slate in order to solve this issue for me. #ceejayoz's solution about making sure if .htaccess is working correctly, was a great help to test .htaccess configuration.
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.
I tried almost everything
but I keep receiving
Method not allowed
Method not allowed. Must be one of:
POST
The code that I'm using is:
$app->post('/ticket/new', function (Request $request, Response $response) {
// Create new book
echo 'a;';
});
I'm trying on postman
method: post
url: /public/ticket/new
header: Content-Type application/javascript
Any clue will help me
.htaccess is ok:
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Thanks a lot
Have you tried something like:
$app->map(['GET', 'POST'],'/ticket/new', function (Request $request, Response $response) {
// Create new book
echo 'a;';
});
define('ROUTE_BASE', 'lumen/public');
$app->get(ROUTE_BASE . '/', function () use ($app) {
return $app->welcome();
});
$app->get(ROUTE_BASE . '/test', function () use ($app) {
return 'test data : 123 abc !';
});
When I access 'localhost/lumen/public/' I can see the 'lumen welcome page'.
But if I try to access 'localhost/lumen/public/test', I receive the following error.
Error: its not found(404).
Laravel expects the public directory to be the webroot of your domain. As this is not true in your case, you will need to make some alterations to your .htaccess.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /lumen/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Also worth noting that instead of using a constant, you can use route groups to achieve the same functionality in your routes.php.
$app->group(['prefix' => 'lumen/public'], function ($app) {
$app->get('/', function () {
//welcome
});
$app->get('test', function () {
return 'test data : 123 abc !';
});
});
your lumen project must be put in webroot of your localhost,domain or virtual host not in subfolder of your webroot without edit your .htaccess.
for access your project in browser : http://lumen.laravel.dev not http://lumen.laravel.dev/public/
I hope this help. sorry for my English :)
My app is on C:\xampp\htdocs\urlrouter\klein\
I installed the klein router using composer.
And, I use this script just for simple basic routing
define('APP_PATH', '/urlrouter/klein/');
require_once 'vendor/autoload.php';
$request = \Klein\Request::createFromGlobals();
$request->server()->set('REQUEST_URI', substr($_SERVER['REQUEST_URI'], strlen(APP_PATH)));
$klein = new \Klein\Klein();
$klein->respond('GET', '/hello', function () {
return 'Hello World!';
});
$klein->dispatch($request);
And I also have this .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
But, when I go to http://localhost/urlrouter/klein/hello , it redirects me to the XAMPP homepage or http://localhost/xampp/splash.php
I can't figure out what's wrong with this router. Please help me
I've never tried to manipulate the REQUEST_URI with Klein (not saying you shouldn't, just warning of a lack of expertise), but your substr() call is going to yield "hello", where your route pattern is "/hello". That might or might not matter (the route matching logic in Klein is somewhat complex, and I haven't internalized all its details). At any rate, I think it's worth a try to define your APP_PATH as '/urlrouter/klein' instead of '/urlrouter/klein/'.
If that works, cool. If not, post a comment and I'll try to reproduce what you're seeing.
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.