Laravel Controller not working - laravel

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.

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.

CodeIgniter 4 - No direct script access allowed

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

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.

PHPUnit Error: Call to undefined method Tests\Unit\ExampleTest::visit()

This is the test case I've written and when i try to run it with this command vendor/bin/phpunit on linux it gives me the error "Call to undefined method Tests\Unit\ExampleTest::visit()"
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testBasicTest()
{
$this->visit('/Login')
->type('KP123#gmail.com','email')
->type('123456','password')
->press('Login')
->seePageIs('/home')
->see('Katy Perry');
}
}
I've tried running composer update and it still could not work. Had Anyone experienced this issue before?
The visit() method looks like it's part of the SeleniumTestCase. So my guess is you either extend from the wrong base class. I assume it should look something like this:
class ExampleTest extends \PHPUnit_Extensions_Selenium2TestCase
{
// your test method
}
edit: I just noticed the Laravel tag, so it's probably more something like Illuminate\Foundation\Testing\TestCase. If this gives you errors that it can't find the class you have to set the bootstrap file in your phpunit.xml to the vendor/autoload.php or another appropriate bootstrap file where the file is registered in the autoloader.
I might have come in late onto this question but here is my 2 cents. I have been trying to use the same methods with my test case which led to a dismal failure.
I the realised that I could actually make use of the TestResponse class which is found on the Foundation namespace that wraps many methods around the response object.
$this->get('/url/to/visit'); //Then you can make your assertions on the returned response.
In my case I had authentication turned on for the application therefore it always complained about the header view where we display the username of the currently logged in user. If you run into that situation then use the Auth Facade like shown below:
\Auth::loginUsingId(1);
You should now be able to assert that your response has some text in it. Hope this helps. By the way I am on Laravel 5.4
If you are using new version of Laravel you must use Laravel Dusk to use similar methods to $this->visit or see. It was excluded from Laravel around version 6.
Instead you can use, but it has limited options:
$this->get('/Login')
composer require laravel/browser-kit-testing --dev
Try this
if you are using > Laravel 8 try :
$this->get('/Login')
instead of visit function, and assertSeeText instead of see

Call to undefined method Illuminate\View\View::make()

I am using Laravel 5.4.16 While using make method of View class i am getting undefined method error.
public function Index()
{
return View::make('stats');
}
Try to add this to the top of the class:
use View;
If it doesn't work then it looks like you didn't install the project. You need to run composer install or composer update command which will download and install all dependencies into the vendor directory.
Another thing to check is config/app.php should have this line:
'View' => Illuminate\Support\Facades\View::class,
Alternatively, you could use helper:
return view('stats');
But it will work only if porject is installed properly.
return view('path.to.your.directory');
You are receiving this error because make() method doesn't exist anywhere in your code. If you want to go from controller to view you can use simply:
public function Index() {
return view('stats');
}
It will go to your status blade view by accessing your url request.
Just use Facade instead. Check how it works and then check what is wrong with View first. And I hope you are not using another template engine as Twig for example, cause if yes then you have definitely use Facade or check how the View factory is working, I think it requires template engine in the constructor ...
Just use use Illuminate\Support\Facades\View;
You can try to use it:
return view('stats');

Resources