I need to redirect '/home' URL to '/' in Codeigniter 4.
The code below does it perfectly, but I wonder if there is a better way to do this inside the Routes.php file.
$routes->get('/', 'Pages::view/home');
$routes->get('/home', function()
{
header('Location: '.'/');
}
);
ANSWER
$routes->get('/', 'Pages::view/home');
$routes->addRedirect('/home', '/');
Related
I want to redirect my page from pure php file to laravel file.
Is there any way to do so?
index.php
< a href="(laravel_file_here) " >Welcome </a>
In public folder suppose you have static_website.php -
<a href='/login'>Go to Login Page</a>
In Laravel routes file located at routes/web.php you can add
Route::get('/login', function () {
return view('login'); // located in resources/views
})->name('login');
This will take you to login page if you click on Go to Login Page in static_website.php page
this is in app directory
'locale' => 'en',
this is in web.php
Route::get('/{locale?}',function($locale=null){
return view('index');
});
Route::get('/about/{lang?}',function($lang=null){
App::setlocale($lang);
return view('about');
});
Route::get('/contact/{lang?}',function($lang=null){
App::setlocale($lang);
return view('contact');
});
// Route::get('/portfolio/{lang?}',function($lang=null){
// App::setlocale($lang);
// return view('portfolio');
// });
I have commented out the portfolio route but it is still working. if i delete this rout it still works
My app's default language is English. but when I setlocale to en or bn images are not showing correctly.{except index route}
http://127.0.0.1:8000/about view
http://127.0.0.1:8000/about/en view
A file path describes the location of a file in a web site's folder structure.
<img src="images/illustrations/leaf-bg-top.png" alt="illustrations">
That means the "images/illustrations/leaf-bg-top.png" file is located in the same folder as the current page. If you give a slash (/) before the path, :
<img src="/images/illustrations/leaf-bg-top.png" alt="illustrations">
That means, images/illustrations/leaf-bg-top.png file is located in the images folder at the root of the current web
In my routes.php file I have the following codes:
$route['admin/login'] = 'admin/login/index';
$route['admin/add_client'] = 'admin/add_client/index';
$route['(:any)'] = function ($val){
require_once( BASEPATH .'database/DB.php' );
$db =& DB();
$db->select('url');
$db->from('interior_form');
$db->where('url',$val);
$query = $db->get()->row();
$db->close();
if(sizeof($query)>0):
return 'home';
else:
return "404_override";
endif;
The issue that I am facing here is whenever I put www.xyz.com/admin/login , it goes to the home page first then again if I write www.xyz.com/admin/login in the same browser, only then it goes to the admin login page.It does not go to the admin login page on the very first instance.
Try:
$route['admin/login'] = 'admin/login/index';
$route['admin/add_client'] = 'admin/add_client/index';
$route['(:any)'] = 'home/$1';
In your home controller, there you handle which request is 404 and which is valid.
You don't need to edit your Rout file.first of all come out from that file.
and you can do this.....
In admin controller page create a function called index.in that function you load your login page.
whenever you type www.xyz.com/admin then load the admin login page.
note:-
Which controller page contains index function,first that index function is load when call that controller, because of index function is first priority compare to other functions in all controllers page.
You may try this simple code for set up rout as bellow.
$route['default_controller'] = 'Adminlogin';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I'm corrently develop webs application for selling products online by using Codeigniter and upgrade from Ci2.2.1 to CI3.0.
However I've meet an errors 404 Page Not Found when I enter domain name on url not yet called any controller by hoped default_controller will load instead of type my main controller.
Notes: It is working on Ci2.2.1 and it stop work when upgrade to CI3.0.
It is working if I type any characters or any Controller after domain name(http://localhost/Ecom3/sometroller) But it is coudn't call default_controller and keep empty or blank after domain (http://localhost/Ecom3/).
Errors log: ERROR - 2015-04-24 03:48:57 --> 404 Page Not Found: /index
Please check my webs structure below:
Main->Main_controller->MY_controller->CI_Controller
My purpose to make it easy to controller the templates.
In MY_controller I've use __autoload() function
function __autoload($class){
if (strpos($class, 'CI_') !== 0)
{
if (file_exists($file = APPPATH . 'libraries/' . $class .'.php'))
{
include_once $file;
}
}
}
And I have confige route as below
$route['default_controller'] = "main/Main";
$route['(:any)'] = 'main/main/index/$1';
$route['c'] = 'cat/cat/index/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
The problem:
My url can't be blank after I type domain or after forword slash as below image
Please help
I try your code and get 404 when Main.php in your controller place inside controller/main folder. but when im move Main.php into controller folder, default controller to Main has working. then im try to debuging and see:
// Is the method being specified?
if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
{
$method = 'index';
}
in core/Router.php
thats mean codeigniter3 are not allowed you to set default_controller in sub folder. you must put default_controller in app/controller/ to make it works.
in config/router.php
change this:
$route['default_controller'] = "main/Main";
TO:
$route['default_controller'] = "Main";
then move your controller file :
app/controller/main/Main.php
TO
app/controller/Main.php
Following the Laravel 4 documentation on Routing, I've been trying to create a domain route that will handle a wildcard subdomain and pass it over to a controller action, but I'm having trouble passing the argument.
Route::group(array('domain' => '{subdomain}.myapp.com'), function()
{
Route::get('/', function($subdomain)
{
die($subdomain);
});
});
If I write the route like this, it will print out the subdomain, whatever it might be. The problem is that I don't want to write the code that handles these situations in the routes.php file, but use a Controller to handle it all, without redirecting from subdomain.myapp.com to myapp.com/controller/action/subdomain.
So, something like this:
Route::group(array('domain' => '{subdomain}.myapp.com'), function()
{
Route::get('/', 'MyController#myAction', $subdomain);
});
How do I pass the {subdomain} argument to the controller in this case?
It seems as though the morning is smarter than the night. I went with a dispatch solution, so if anyone else has a more elegant solution, please feel free to post and I'll accept your answer instead.
Route::group(array('domain' => '{subdomain}.myapp.com'), function()
{
Route::get('/', function($subdomain) {
$request = Request::create('/myRoute/' . $subdomain, 'GET', array());
return Route::dispatch($request)->getContent();
});
});
Route::get('myRoute/{subdomain}', 'MyController#myAction');