CodeIgniter 4 - No direct script access allowed - codeigniter

After installing and configuring CI 4 I can see the home controller and the debug bar as normal (I'm in development mode).
There is a problem with error reporting. If I try to deliberately write some wrong PHP code, CodeIgniter overrides the normal PHP behaviour and hides the errors (in development mode!!).
Here's an example of deliberately adding a PHP error to the home controller:
<?php namespace App\Controllers;
class Home extends BaseController
{
public function index()
{
this is an error!! ()
return view('welcome_message');
}
}
Here's the output (which is not helpful with real errors):
No direct script access allowed
How can I set CodeIgniter 4 to show PHP errors? (the error is not even logged anywhere inside the server)

this problem happened when u delete this folder (app/view/errors) from view

Related

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

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.

Returns 404 even if MVC files are setup properly in Codeigniter

My code igniter project is returning 404 whenever I use method that I created in controllers. I have it running on Mac OS Mojave on Apache2.
There might be configuration or setting that I missed.
The codeigniter page works fine when opening http://localhost/myproject where I see the welcome page.
I have a 404 problem when I start using the new controller class. To make it simple, I created below "Pages.php" controller file inside application/controllers folder:
<?php
class Pages extends CI_Controller {
public function view()
{
echo "running the view method";
}
}
I expected the page to display "running the view method"
but the result is a 404 page at http://localhost/myproject/view or http://localhost/myproject/index.php/view.
it works by adding below in route.php
$route['(:any)'] = 'pages/$1';

Laravel 5 This webpage has a redirect loop ERR_TOO_MANY_REDIRECTS

I'm working on a project on my local machine, and I use XAMPP for OSX
I used to be able to go to localhost/projects earlier today, but I don't know why am I keep getting the redirect loop now. When I take a look at the Networks tab in Chrome, it's redirecting back and forth between projects and projects/.
I've been trying to find redirect in my ProjectsController, but there's none. In fact, I'm using Route::resource('projects', 'ProjectsController'). Has anyone had this before? how do I solve it
I even tried to simply return a string in my controller
class ProjectsController extends controller {
public function index()
{
return "index";
...
}
...
}
Yet this still giving me redirect loop
It gives me status code 301 if that helps
Update ==
Here's my route:list

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.

Resources