Codeigniter 4 - controllers won't work unless I add them directly to Routes.php - codeigniter

Can anyone tell me:
Do I have to declare all of my controllers in Routes.php in Codeigniter 4?
I can't seem to get a controller to work unless I add it directly to the "Routes.php"
I have created my controllers properly and the Home controller is working after install and setup.
If I add the controller My_page.php :
<?php
namespace App\Controllers;
class My_page extends BaseController{
public function index(){
echo "Controller 'My_page' -> function index() ";
}
}
?>
I get a
: "404 - File Not Found
Sorry! Cannot seem to find the page you were looking for."
If I now add the controller to the rout - i.e.:
$routes->post('my_page', 'My_page::index');
Then my controller works properly and I get the "Controller 'My_page' -> function index() " when I visit www.mydomain.com/my_page
I have also tested:
www.mydomain.com/index.php/my_page
and this makes no difference.
I am using the .htaccess that comes with the download. I have updated the base URL to www.mydomain.com/
The documentation is confusing to me - https://www.codeigniter.com/user_guide/incoming/routing.html#setting-routing-rules ;
it sounds like they are saying have to declare all classes with routes?
Why are my controllers not working without declaring them specifically in Routes.php?
And am I misunderstanding 'setAutoRoute(true)' it also does not seem to work - I expect that I can turn this on and simply create my controllers pretty much like in CI3?

If you don't enable auto-routing you most certainly need to add all routes which you are allowing, anything else will fail with error 404. As #parttimeturtle mentioned - autoroute it is disabled by default since 4.2.
So in short - Yes, you need to add all controllers, their functions and the appropriate http methods. (That includes CLI routes as well)
You can use $route->add(), which will allow all http methods, it's however more secure to explicitly set them with their methods.

Related

Route [add.sp] not defined

i wrote url like that
use App\Http\Controllers\DashController;
Route::get('/admin/special', [DashController::class, 'addSpecializations'])->name('add.sp');
this is controller
public function addSpecializations()
{
return view('dashboard.add-specializations');
}
when i tried to open it i can't even though all route work
after that i wrote this code in view's file
<a href="{{route('add.sp')}}">
so i faced this issue
Route [add.sp] not defined.
In case your routes are cached, run php artisan route:clear. In development, don't cache anything, including views and config.

Redirection issue in Codeigniter4

I have done admin controller and put that in a sub folder named 'Admin'
Controller
Admin
-login.php
Now I want to fetch that by router file where I wrote this
$routes->get('admin', 'Admin/Login::index');
But it is showing me "Not found" error and redirects to "http://localhost/admin".
Could there be some .htaccess issue?
replace this
$routes->get('admin', 'Admin/Login::index');
with
$routes->get('admin', 'Admin\Login::index');
also make sure you add namespace in your login.php
namespace App\Controllers\Admin;
If you keep CI4's directory structure intact you could in fact use sub-folders for Controllers, Models, Views, etc.
For example app/Controllers/Admin/Login.php is a valid place to put a Controller class. Make sure to add the appropriate namespace in Login.php - namespace App\Controllers\Admin; Also in routes - $routes->get('admin', 'App\Controllers\Admin\Login::index'); It is quite possible to work without the prefix of App\Controllers, but I never extensively tested it and I think there was a problem in some versions of CI4 before.
Another issue could be your app/Config/App.php class. If you did not change anything in your .htaccess file (the one in public directory!), $baseURL should be set to your public directory address - http://localhost/myproject/public/ . Or if you wish to make it easier - set up virtual hosts.
Just a thing to add - get() method in $routes allow only GET requests, meaning if you are trying to POST something (or use any other HTTP request method) it will fail and redirect.

Controllers and ajax in laravel 4

I'm new at Laravel and I can't figure how to handle controllers (and ajax).
I have a button in a sidebar, and I want to show a page when it's clicked.
I have a view (which is the page i want to display in ajax) located in views/logs/system.blade.php
and a controller located in controllers/LogsController which has the following code -
class LogsController extends BaseController {
public function getLogs() {
return View::make('logs/system');
}
}
my routes.php has the code -
Route::get('/', 'HomeController#showWelcome'); // Works fine
Route::get('logs', 'LogsController#getLogs');
First things - how can I access the view I'm gettings in getLogs in a URL (localhost/mysite/public/logs doesn't work...)
Second - how can I access it in an ajax call?
I tried
$.get('logs', function(data) {
console.log(data);
});
but it doesn't work either. It gets 500 Internal Server Error....
Help please!
You should be able to go to: localhost/mysite/public/logs
if not, enable mod_rewrite in your apache and in your apache httpd.conf, set:
AllowOverride All
Most probably the server error 500 (in both cases) is caused by the fact that you have a mistake in the View::make() call. To utilize a view in subfolder you have to use the dot notation.
So correct the code
class LogsController extends BaseController {
public function getLogs() {
return View::make('logs.system');
}
}
and you should be good to go, the url should load fine, both in browser and in Ajax.
If you still have problems, check the Laravel Logs (probably path/to/app/storage/logs/...) as well as Apache Error Log (probably /var/log/apache2/error.log). I assume you are using Unix/Linux operating system.
The rewrite module was on.
I solved it by going to localhost/mysite/public/index.php/logs, this is the URL that it expects, maybe something in the .htaccess file is wrong.

Laravel Controller not working

I'm very new to the Laravel framework and am trying to load a simple controller in my browser to slowly get the hang of things.
I have a file that's titled users.php inside of the the laravel/app/controllers/ folder and it looks like this:
class UsersController extends BaseController
{
public $restful = true;
public function action_index()
{
echo 'hi';
}
}
In the routes.php file, I have
Route::get('users', 'UsersController#index');
But, when I go to
http://localhost:8888/laravel/public/users
I'm greeted with a message that says "ReflectionException
Class UsersController does not exist"
I'm not sure if this is because I didn't install the mcrypt extension of PHP. But, when I checked the php.ini file on MAMP, it said that it was enabled. Upon entering
which PHP
in my terminal, it said /usr/bin/php. So, it might not be using the correct version of PHP.
I'm not entirely sure if this is a routes problem or if it's stemming from an absence of a vital PHP extension.
Thanks a bunch!
You need to use the Route::controller method to reference your Controller:
Route::controller('test', 'TestController');
...and rename your file (as Cryode mentions ) to be TestController.php.
Note - if you want to use the filename as test.php, then you will need to use composer to update the autoload settings.
Finally, the format of names for Controller methods changed in Laravel 4, try renaming the method
public function action_index() {}
to be
public function getIndex() {}
the get represents a HTTP GET request... the same applies for post (HTTP POST) and any (GET or POST.. )
I'm not familiar with that part of Laravel's source, so I'm not entirely certain that this is the issue, but your controller file name should match the controller class name, including capitalization.
So users.php should be UsersController.php. Now, when I do this myself on purpose, I get a "No such file or directory" error on an include() call, so that's why I'm not certain that's the sole cause of your problem. But it may be a start.

Setting up Codeigniter HMVC with tank auth

I am having trouble getting a working Codeigniter version 2.0.3 with hmvc and tank auth(set up as a module) setup properly. I have installed CI properlly and then install HMVC with these directions https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
I get to my welcome controller/view as example just fine which means the HMVC is working. Next I try to add tank auth to the project by adding it to a folder in the modules folder. It has the proper controller/view/model etc.. setup within the tank auth. I even added in routes something like
$route["auth"]="auth/login";
I also extended the controller within the auth module to MX_Controller like as directed. Also in the constructor I have :
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('security'); <--failing to load this
$this->load->library('tank_auth');
$this->lang->load('tank_auth');
$this->form_validation->CI =& $this;
It seems to be redirecting fine to the module however it comes up with an error saying ::
An Error Was Encountered
Unable to load the requested class: security
What am I doing wrong? Does any one have a working CI installation with HMVC and tank auth as a module so I can see how its done? I'm new to HMVC, thanks
I found the same problem, but i solved by simple adding a comment to the
$this->load->library('security');
so it will look like this:
//$this->load->library('security');
since security its now part of the codeigniter core, i guess its already loaded by default, and everything seems to be working pretty good
it is in Helper now according to CodeIgniter 3.0 user guide
try:
$this->load->helper('security');
I fix this, by creating Security.php file in application/libraries directory with the following code:
require_once(BASEPATH.'core/Security.php');
class Security extends CI_Security { }
I found a solution, I simply took the security.php file from codeigniters system/core folder and dropped it into system/libraries.
move the file security.php from system/core to system/libraries
then edit core/codeigniter.php line number 204 from $SEC =& load_class('Security', 'core'); to $SEC =& load_class('Security', 'libraries');
Security.php is present in "codeigniter/system/core/Security.php"
so provide this path your problem gets solved easily
load->library('../core/security');
I Read CodeIgniter 3.X User Guide and I was found that "Security" is available as a 'helper' Now.
So you need to change this;
$this->load->library('security');
into
$this->load->helper('security');
XSS Filtering
The Input class has the ability to filter input automatically to prevent cross-site scripting attacks. If you want the filter to run automatically every time it encounters POST or COOKIE data you can enable it by opening your application/config/config.php file and setting this:
$config['global_xss_filtering'] = TRUE;
You need to read a CodeIgniter 3.0 User Guide there are so many changes and implementation or please Refer change log.

Resources