Directories in codeigniter controller - codeigniter

I have two directories within my codeigniter controller.
/controllers/admin/
dashboard.php
content.php
enquiries.php
/controllers/members/
profile.php
chat.php
settings.php
Because the directory folders are not themselves controllers I can't perform any functions if the user browses to the root of the directory.
Example if the user browses to
/localhost/admin/
a view won't be loaded and will not show a 404. This lets users know that the directory does exist, giving me a security risk because people will know that I have an admin directory.
How is it possible to show a 404 message if the user browses to the root of the directory folder???

You could add this in config/routes.php:
$route['admin'] = 'errors/page_missing';
$route['members'] = 'errors/page_missing';
... where Errors is a controller with a method of page_missing, where you load a 'file not found' view.

The way I do it is creating the default controller welcome.php in the directories I wan't to hide and add the show_404() function. Like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
show_404();
}
}
This way, you don't have to fiddle with routes or rewrite rules. Simple and clean.

Related

Route to a specific controller in codeigniter

I am using codeigniter for the back end of my website.
For that I have created admin folder inside my controller.
My issue is that when i type the url
http://localhost/varma/admin
It should redirect to the file login.php which is inside the contollers/admin
Inside the routes.php write this code:
$route['admin'] = 'admin/controller/function';
Try this code in your routes.php file ,
$route['admin'] = 'admin/login/your_login_function_name';
$route['admin']='admin/login';
where login the is login function name

How to access non-default view inside view folder in code igniter

I know how to display default view in Code Igniter. But, how can I access other files inside view folder. Example I want to display 'application/views/admin/index.php' but it isn't default view.
You can add sub folders in view like so.
$this->load->view('admin/index');
Is that what your meaning.
Codeigniter Views
File name: Example.php
<?php
class Example extends CI_Controller {
public function index() {
$this->load->view('admin/index');
}
}
Folder Structure
application
application / views /
application / views / admin
application / views / admin / index.php
I would not choose index.php for a name for a view I would rename it to another name dashboard.php or some thing. Because you have a index.php in the main directory.

I have weird behaviour in laravel 5 it redirects me to root when put customers in url

I have weird behaviour in laravel 5, it redirects me to root when put customers in url.
like this:
localhost:8080/easy_marketing/public/customers
it redirects me to
http://localhost:8080/customers
also,
when use words like those: customers_, customer, _customers they worked fine.
another links like
localhost:8080/easy_marketing/public/groups
localhost:8080/easy_marketing/public/keywords
they are working fine.
routes.php
Route::get('/', 'WelcomeController#index');
Route::get('home', 'HomeController#index');
Route::group(['middleware' => 'client'],
function() {
Route::get('customers', 'users\CustomersController#index');
Route::get('customers/import', 'users\CustomersController#import');
Route::post('customers/run-import', 'users\CustomersController#runImport');
});
Routes cannot have the same name as any folders in your public directory.
Laravel will redirect to the root if you try to access a route when you have a folder with the same name in your public folder.
Credit to user #Lazirro for his answer here: Laravel slash after url redirects to root folder
I re-name customers folder in public folder and the link is working ok.

Route to admin page in codeigniter

I'm working with Codeigniter and i want to have a URL like this:
admin.domain.com
And then route it to the real controller and function.
It means I don't want to have a URL like this : www.domain.com/admin.
I have one controller for back-end and another for front.
This is my default controller in route file:
$route['default_controller'] = "home";
so how should i do it? with route file or htaccess file or some another way?!

CodeIgniter URL routing - SEO friendly URL

I've created a new route for my site:
$route['default_controller'] = "welcome";
$route['(:any)'] = "welcome/index/$1";
$route['404_override'] = '';
And this works fine in my site when URL is like this:
http://mydomain.com/first-article
http://mydomain.com/second-article
*my controller is just welcome.php
but
i also have a controller for the admin and the URL for the admin is:
http://mydomain.com/admin
What will I add to the routes file to ignore the /admin and other controllers inside the admin?
You can replace the welcome/index/$1 route with:
$route['^(?!admin).*'] = "welcome/index/$1";
This basically says, that if a URI that does not start with "admin" should route to the welcome/index method and pass the contents to the index method. Otherwise, handle normal routing with admin being the controller.
Open application/config/router.php and change the
$route['404_override'] = '';
to
$route['404_override'] = 'router/index';
You can use all controllers as the normal way.
When you try to use a controller that is not exists, you should route it to a 404 controller.
Create a controller named Router.php as a controller structured for CodeIgniter.
In the index method inside Router.php, query for related sef url and do required operations. All requests that routes to an undefined controller will be handled by router/index method. Others will be redirected to related controller as usual.
You might want to use header codes to point that related page is not 404.

Resources