I use Slim php framework to build some action inside index.php page:
so this is the way I call my action:
index.php/action1
$app->get('/action1',function () use ($app){
echo "this is action 1";
});
index.php/action2
$app->get('/action2',function () use ($app){
echo "this is action 2";
});
Now I want my url become pretty , such as when type in index/action1
, it will redirect to index.php/action1
Please provide me the solution in creating htaccess to do thatThank and best reagard
Create .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
user group to prefix your route
$app->group('/index', function () use ($app) {
/**
* Route GET /index/action1
*/
$app->get('/action1',function () use ($app){
echo "this is action 1";
});
});
Related
When I go to http://www.example.com/new/index.php/login/
(please note /index.php/ as part of the url.)
After successful login, I get redirects to http://www.example.com/new/welcome/
and that's correct.
but the login screen url should not have /index.php/ as this is Silex restapi.
But when I try login without /index.php/ that would be
http://www.example.com/new/login/
after login this time, I get redirected to new/index.php instead of /welcome/ like last time.
Please help.
my code is below:
Index.php:
$app = Silex\Application;
$app->mount('/login', new Routers\Login());
$app->run();
Routers\Login.php:
namespace Routers;
use Silex\Application;
use Silex\Api\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request ;
class Login implements ControllerProviderInterface
{
public function connect(Application $app)
{
// creates a new controller based on the default route
$controllers = $app['controllers_factory'];
$controllers->get('/', 'Controllers\\Login::index');
$controllers->post('/', 'Controllers\\Login::validate');
return $controllers;
}
}
Controllers\Login.php:
namespace Controllers;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
class Login {
public function index(Request $request, Application $app)
{
return $app['twig']->render('login.html');
}
public function validate(Request $request, Application $app)
{
// validation goes here
if ( // invalid ) {
return $app['twig']->render('login.html');
} else {
// valid
header("Location: /welcome");
exit;
}
}
}
htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>
EDIT:
I guess I discovered the issue, the login form is:
<form method="post" action="index.php">
instead of posting data to http://www.example.com/new/login
So how the action url must be? I tried action="/new/login" and it doesn't work. I get no route for POST /login. but this is defined in Routers/Login.php, so why should I get this?
Please advise.
EDIT2:
How can I have named routes in my Routers\Login.php as I am using organized controllers with mount like
$controllers->get('/', 'Controllers\\Login::index');
it seems it doesn't accept bind()? Does organized controllers support named routers?
If you want to let silex find the right route for you,
bind a name to your route declaration:
$controllers->get('/', 'Controllers\\Login::index')->bind('login');
and use it in your twig template
<form method="post" action="{{ path('login') }}">
It should find the url you need.
i have a little problem with slimphp router :
$app->group('/api', function () use ($app) {
// Library group
$this->group('/library', function () use ($app) {
// Get book with ID
$this->get('/books/:id', function ($req, $res) {
echo "books";
});
// Update book with ID
$this->put('/books/:id', function ($req, $res) {
});
// Delete book with ID
$this->delete('/books/:id', function ($req, $res) {
});
});
});
Sending A GET to /api/library/books/1 give me a Page not found Error, where is the problem.
EDIT :
.htaccess
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]
PS: a simple app->get is working without any problem,
It is not found as Slim 3 uses {id} as placeholders, not :id as Slim 2 did. Therefore the code would be
$this->get('/books/{id}', function ($request, $response, $args) {
...
});
Found in Slim 3 Documentation for Route Placeholders
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 :)
I am working on a site in CodeIgniter.
I want my site users to select the city from a drop-down and then the content to be displayed on the basis of that city. For this I have two issues.
1) How can I add a new parameter to the URL segment. I checked this . Is it ok to create city as controller? but then how should I create the current controllers? If so,
2) Problem is I have already worked on all controllers.
Guide me on how should I proceed.
You could pass the uri segments as parameters to your controller.
http://YOUR_URL.com/index.php/city/get/zurich
<?php
class City extends CI_Controller {
public function get($city)
{
echo $city;
}
}
http://www.codeigniter.com/user_guide/general/controllers.html#passing-uri-segments-to-your-methods
Edit
Just to give you an idea:
First remove the index.php from the URL:
create the .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# When CI is in a subfolder use this line instead
#RewriteRule ^(.*)$ /ci/index.php/$1 [L,QSA]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Open the file /application/config/config.php and search for the line
$config['index_page'] = 'index.php';
and change it to
$config['index_page'] = '';
Open the file /application/config/routes.php and add this line to the other rules
$route['(:any)/(:any)/(:any)'] = "$2/$3/$1";
And the controller looks like this.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class City extends CI_Controller {
public function index($city)
{
echo $city;
}
}
So I'm just changing the order of the segments. 2 = Class, 3 = Method, 1 = parameters.
try this.
-first tell me how u send request after selecting a city from dropdown.
-anyways i will tell you ,first add jquery 'change' event to dropdown list and on change you have to get the current value of the dropdown list as
e.g
$('#idOfDropDownlist').on('change',function(){
var value = $(this).val();
//now send a ajax request to controller to get information about city.
$.ajax({
type:'get',
url:"<?php echo base_url(); ?>ControllerName/methodName/"+value,
success:function(response){
console.log(response);
}
});
});
//---- your controller's method for getting this request will be like.
class ControllerName .....{
public function methodName($city = ''){
//--- here you got the city name,so do whatever u want with......
}
}
I have a problem. I am using slim and I have route for my main page:
$app->get('/', function() use ($app) { ...
In one of my controllers I want to redirect to the main page, so I write
$app->response->redirect('/', 303);
But instead of redirection to the '/' route I'm getting redirected to the root of my local server which is http://localhost/
What am I doing wrong? How should I use redirect method?
Slim allows you to name routes, and then redirect back to them based upon this name, using urlFor(). In your example, change your route to:
$app->get('/', function() use ($app) { ... })->name("root");
and then your redirection becomes:
$app->response->redirect($app->urlFor('root'), 303);
See Route Helpers in the Slim documentation for more information.
From Slim3 docs
http://www.slimframework.com/docs/start/upgrade.html
$app->get('/', function ($req, $res, $args) {
return $res->withStatus(302)->withHeader('Location', 'your-new-uri');
});
Slim 3
$app->get('/', function ($req, $res, $args) {
$url = 'https://example.org';
return $res->withRedirect($url);
});
Reference: https://www.slimframework.com/docs/v3/objects/response.html#returning-a-redirect
give your '/' route a name,
$app = new \Slim\Slim();
$app->get('/', function () {
echo "root page";
})->name('root');
$app->get('/foo', function () use ($app) {
$app->redirect($app->urlFor('root') );
});
$app->run();
This should give you the correct url to redirect
http://docs.slimframework.com/routing/names/
http://docs.slimframework.com/routing/helpers/#redirect
//Set Default index or home page
$app->get('/', function() use ($app) {
$app->response->redirect('login.php');
});
Slim 4:
$response->withHeader('Location', '/redirect/to');
Or in place of fixed string:
use Slim\Routing\RouteContext;
$routeParser = RouteContext::fromRequest($request)->getRouteParser();
$url = $routeParser->urlFor('login');
return $response->withHeader('Location', $url);
Slim Documentation: http://www.slimframework.com/docs/v4/objects/response.html#returning-a-redirect
RouteContext: https://discourse.slimframework.com/t/redirect-to-another-route/3582
For Slim v3.x:
Use $response->withStatus(302)->withHeader('Location', $url); instead of $app->redirect();
In Slim v2.x one would use the helper function $app->redirect(); to trigger a redirect request. In Slim v3.x one can do the same with using the Response class like so (see the following example)[1].
Use pathFor() instead of urlFor():
urlFor() has been renamed pathFor() and can be found in the router object.
Also, pathFor() is base path aware[2].
Example:
$app->get('/', function ( $request, $response, $args ) use ( $app ) {
$url = $this->router->pathFor('loginRoute');
return $response->withStatus(302)->withHeader('Location', $url);
});
Note: additional parameters can be supplied by passing an associative array of parameter names and values as a second argument of pathFor() like: $this->router->pathFor('viewPost', ['id' => 1]);.
The router’s pathFor() method accepts two arguments:
The route name
Associative array of route pattern placeholders and replacement values[3]
References:
Changed Redirect
urlFor() is now pathFor() in the router
Route names
I think using ./ instead of / will work also.
I think I faced a similar problem, and the issue was with my .htaccess config file. It should actually be something like this:
RewriteEngine On
RewriteBase /api #if your web service is inside a subfolder of your app,
# you need to pre-append the relative path to that folder, hope this helps you!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]