Form validation class not recognised Laravel 5.1 - validation

I'm trying to validate an email field on a Post request in Laravel 5.1.
On my Controller i have 'use App\Http\Requests;' but I get an error message 'Class App\Http\Controllers\StorePotentialUserRequest does not exist'. (For some reason it's looking for the class in the controller).
The class IS found when i have 'use App\Http\Requests\StorePotentialUserRequest;'. But I feel that this shouldn't be the case..
I also can't 'use App\Http\Requests\Request' because there is a conflict with 'use Illuminate\Http\Request'.
My code in the controller is as follows:
public function create(StorePotentialUserRequest $request)
{
...
EDIT: works if you namespace the code class as follows:
public function create(\App\Http\Requests\StorePotentialUserRequest $request)
{
...
But this is not mentioned on the documentation

Unless you are in the same namespace, you do have to specify where in the namespace are the classes you are using.
Either with use or with the full path every time you refer it.

Related

Unidentified Controller when using a route that calling a controller

Good day, and thank you for reading this problem
I have a problem where I'm using a different parameter but it doesn't work, here's the problem code
Route::get('/profiles','ProfilesController#index');
But when I'm using this code it worked perfectly fine
Route::get('/profiles',[ProfilesController::class, 'index']);
Here's the controller
class ProfilesController extends Controller
{
public function index()
{
return profiles::all();
}
}
You need to use full namespace App\Http\Controllers\ProfilesController#index
use App\Http\Controllers\ProfilesController;
// Using PHP callable syntax...
Route::get('/profiles', [ProfilesController::class, 'index']);
// Using string syntax...
Route::get('/profiles', 'App\Http\Controllers\ProfilesController#index');
If you would like to continue using the original auto-prefixed controller routing, you can simply set the value of the $namespace property within your RouteServiceProvider and update the route registrations within the boot method to use the $namespace property.
More info:
https://laravel.com/docs/8.x/upgrade#automatic-controller-namespace-prefixing

Target class does not exist in Laravel

this is the route from api.php :
Route::apiResource('friend-request', FriendRequestController::class)->except('delete');
the controller already exists and i'm calling it on a store using a post request :
FriendRequestController.php
class FriendRequestController extends ApiController
{
public function store(StoreFriendRequest $request, FriendRequestService $friendRequestService,UserService $userService)
{
//code
}
i get this error after calling this route
message: "Target class [App\Http\Controllers\App\Http\Controllers\FriendRequestController] does not exist."
i guess it's because of the form request what do you think ?
This is very probably because the $namespace property of the app/Providers/RouteServiceProvider.php file is uncommented.
It means that every controllers in your route file will have the default \App\Http\Controllers namespace prefixed.
So \App\Http\Controllers\FriendRequestController becomes \App\Http\Controllers\App\Http\Controllers\FriendRequestController and so on...
Since you are using the "new" route declaration notation (using the fully qualified namespace), you don't need a "default" namespace:
Route::apiResource('friend-request', FriendRequestController::class)->except('delete');
// FriendRequestController::class === \App\Http\Controllers\FriendRequestController
// However in the old declaration:
Route::apiResource('friend-request', 'FriendRequestController')->except('delete');
// You were only using the base class name, so the prefix was here to help Laravel find the "entry point" of your controllers
// which is \App\Http\Controllers\
All this is detailled in the migration guide: https://laravel.com/docs/8.x/upgrade#automatic-controller-namespace-prefixing

How to work with subdirectory controllers in CodeIgniter 4?

I need help with using sub directory controllers in CodeIgniter 4.
I just can't make it work for some reason.
This is the URL for example: www.example.com/admin/dashboard
In the controllers folder, I created a folder named Admin, and a file named Dashboard.php.
I used this code in Dashboard.php:
namespace App\Controllers;
class Dashboard extends BaseController
{
public function index()
{
}
}
I tried changing the class name to AdminDashboard, Admin_Dashboard, pretty much every logical name but every time I get a 404 error, saying:
Controller or its method is not found:
App\Controllers\Admin\Dashboard::index
I know the file itself gets loaded successfully, but I think I don't declare the classname correctly and it keeps throwing me those 404 errors.
The documentation of CI4 isn't providing any information about what the classname should be called unfortunately...
UPDATE #1
I managed to make it work by changing few things:
namespace App\Controllers\Admin;
use CodeIgniter\Controller;
class Dashboard extends Controller
{
public function index()
{
}
}
But now it won't extend the BaseController which has some core functions that I built for my app.
Any ideas to how to make it extend BaseController?
I must admit that I don't have much knowledge about namespacing yet, so that might be the reason for my mistakes.
As I imagined, the problem was that I didn't learn about namespacing.
I needed to point the use line at the BaseController location.
namespace App\Controllers\Admin;
use App\Controllers\BaseController;
class Dashboard extends BaseController
{
public function index()
{
}
}
Now www.example.com/admin/dashboard/ goes directly to that index function, as intended.
php spark make:controller /Subfolder/ControllerName
$routes->add('/(.+?)_(.+?)/(.+?)$', 'subdir\\\\$1_$2::$3');
$routes->add('/(.+?)_(.+?)$', 'subdir\\\\$1_$2::index');
I was able to map with this setting.
The route mapping could be as simple as:
$routes->group('admin', static function ($routes) {
$routes->get('dashboard', 'Admin\Dashboard::index');
});

Laravel Dependency Injection issue with Controller?

I setup the dependency injection as I felt it should go into my Patient Controller but for some reason it will never execute the dependency on the index function on the last line, it will even return the $request data before it but for some reason will not execute the data in the Patient Repository I attempt to return.
I have attempted to just do:
return (new Patient)->getByAccNumAndDateOrZip($this->client_code, $this->account_number, $this->dob, $this->zip);
Also, keep in mind yes all $requests do have a valid value and do not return a empty or null value.
And still get nothing back....
namespace App\Http\Controllers\api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
//Repositories
use App\Repositories\Patient;
class PatientController extends Controller {
private $patient;
public function __construct(Patient $patient) {
$this->middleware('auth.client');
$this->patient = $patient;
}
public function index(Request $request) {
//I can do return $request->client_code
//But I can't use this dependency... It's weird...
return $this->patient->getByAccNumAndDateOrZip($request->client_code, $request->account_number, $request->dob, $request->zip);
}
}
I'm expecting to call to my dependency that pulls all the my patients by account number. The dependency is just a standard class with a namespace of App\Repositories it has no set constructor just a couple standard public functions that will take specific variables in the function.
After reviewing the log files in laravel I was able to see that the system was stating that the class name has already been used so I would have to choose something else.
This was suggested by Matt Wohler to check the logs and boy did it help me!
Thanks Again for all the help!

Extending Form Validation in Codeigniter call to a member function on a non-object error

I know this has been asked before, and I've looked through every answer posted, but to no avail. Basically, I am trying to extend the Codeigniter form validation library on a CI2+ project, but every single time I get the error:
Fatal error: Call to a member function get_errors_array() on a non-object
Below is my code:
application/core/CB_Form_validation.php
class CB_Form_validation extends CI_Form_validation{
function __construct($rules = array()) {
parent::__construct($rules);
log_message('debug', "CB_Form_validaiton class initialized");
}
public function get_errors_array() {
return $this->_error_array;
}
}
and then in my ajax file I have the construct etc.
public function save(){
if($this->form_validation->run() == FALSE){
}
}
and inside that if statement I have tried the following:
echo $this->form_validation->get_errors_array();
echo $this->cb_form_validation->get_errors_array();
echo get_errors_array();
I have also tried placing the CB_Form_validation.php in application/libraries as well. Just as well, I have also tried this in my construct of my ajax controller
$this->load->library('CB_Form_validation');
$this->load->library('core/CB_Form_validation');
And I have set CB_ as my custom class prefix
Turns out that to fix this, you should do the following:
Move your custom form validation class to the application/libraries folder
You can keep the construct of your custom class how it is
Use $this->form_validation->method() to access the function you would like
As long as your loading the form_validation library, you don't need to perform $this->load->library('custom_class') because CI picks up on your custom prefix anyways
Hope this helps someone

Resources