I am very new in Laravel and this is my first project. I have this problem and I don`t know how to fix it.
This is my directory
I am sure I have tried all I know
This is my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class EditareProfilController extends Controller
{
public function __construct()
{
$tasdhis->middlewasdare('auasdth');
}
public function index()
{
return vasdasdiew('editaasdasdasdeprofil');
}
}
and this is my route:
Route::get('/editarasdasdeprofil', 'EditareProfilasdasdasdontroller#index')->name('editarepasdasdasdrofil');
The default location for controllers is app\Http\Controllers if you add a folder just add it to your route in order to laravel localize it. In your code you are storing your controller under the Auth folder.
Update your route to:
Route::get('/editareprofil', 'Auth\EditareProfilController#index')->name('editareprofil');
And also don't forget to update the namespace.
namespace App\Http\Controlllers\FOLDER;
Related
I have a class called CustomerController with a delete function:
class CustomerController extends Controller
{
public function getAllCustomer()
{
return \App\model\Customer::get();
}
public function destroy (Customer $id)
{
$id->delete();
}
This is the route:
Route::delete('customer/{id}' , 'CustomerController#destroy');
I get this error:
Class App\Http\Controllers\Customer does not exist
I already tried Composer update and Composer dump-autoload with no success.
A screenshot:
Thank you very much!
When you do not include classes using use statements, php will try to find the class in the current namespace.
So, the function function destroy (Customer $id) will look for the class Customer in the App\Http\Controllers namespace. To avoid this, add a use statement for the App\model\Customer class above on top of your controller class. For example:
<?php
namespace App\Http\Controllers;
use App\model\Customer;
class CustomerController extends Controller
{
public function getAllCustomer()
{
return Customer::get();
}
public function destroy (Customer $id)
{
$id->delete();
}
}
Now you can also use a shorter name in the getAllCustomer() function.
Change this :
return \App\model\Customer::get();
to this:
add namespace at the top:
use App\Customer;
return Customer::get();
as you are using route model binding on delete action by injecting Customer class to the method therfore its failing so make sure you add model namespace at the top of the file(CustomerController).
The error is happening beacuse CustomerController is trying to look for Customer model in controllers namespace which means that your model namespace is wrong.
Where to save new model file created in laravel 5.3 in /app/http folder or in /app folder
And how I will call the method in it from controller ?
How to include the model in controller?
Laravel 5.3
Save them in app/models folder and you can access it by constucting it,Make sure you have ran composer dump-autolad
public function __construct(YourModel $model) {
parent::__construct();
$this->model = $model;
}
in your code simply model->test();it should work.
You create the folder Modelswherever you want and add your files as you wish, just make sure to properly namespace it. As long as Composer can autoload the class.
You can manually create app/Models/User.php and just make sure to namespace it:
namespace App\Models;
class User {
// your code
}
And to call the model method from a controller use the namespace of the model:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\YourModel;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class YourModelController extends Controller
{
public function whatever(){
$yourmodel->method();
}
}
I created a Panel directory inside Controller directory .
there is a login function inside AdminController.php
class AdminController extends Controller
{
//
public function login()
{
return 'test';
}
}
in routes.php I wrote a route like this:
Route::get('/cp/login','Panel\AdminController#login');
but when I run below url I got some errors that there isn't exist this controller :
http://localhost:8000/cp/login
ReflectionException in Route.php line 280: Class
App\Http\Controllers\Panel\AdminController does not exist
Try adding the appropriate namespace to the top of the AdminController file, you will also need to specify the namespace for the Controller class that it extends, as they are under different sub-namespaces.
You can read more about PSR-4 autoloading here http://www.php-fig.org/psr/psr-4/.
Based on the directory structure that you have there it should read
<?php
namespace App\Http\Controllers\Panel
use App\Http\Controllers\Controller;
class AdminController extends Controller {
//..
}
you should add the namespace to the contorller
Change you namespace to
namespace App\Http\Controllers\Panel;
Laravel will resolve controllers based on your name spacing, not on your directory structure.
My Controller for my class 'Article' cannot be found.
I am needing to fetch all entries from the article table.
I am able to use the DB:: facade to pull what's in the DB, but when I try using Article::all() I get:
Class 'App\Http\Controllers\Article' not found
in ArticleController.php line 15
at Application->Laravel\Lumen\{closure}()
Line 15 looks like:
$article = Article::all();
This is what I've tried so far, but with no success:
updated .env.example to .env and set up my DB credentials.
in bootstrap/app.php I've uncommented Dotenv::load(__DIR__.'/../');
In bootstrap/app.php I've uncommented $app->withFacades(); $app->withEloquent();
I've tried to use the full route of the controller in routes.php: $app->get('article', 'App\Http\Controllers\ArticleController#index');
My model directory is located under app->Models and has my Article.php model:
<?php
# app/Models/Article.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $table = 'articles';
protected $fillable = ['title', 'content'];
}
My Controller is ArticleController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ArticleController extends Controller{
public function index(){
$article = Article::all();
return response()->json($article);
}
}
And my routes.php
<?php
$app->get('article', 'ArticleController#index');
I really appreciate any help on this error. I've unfortunately spent the better part of 2 days on this.
Thanks.
You need to have the proper namespace. Your Article model is in the App\Models namespace so you need to add this to the top of your controller:
use App\Models\Article;
Just run composer update. a new autoload classmap will be generated and all your new composer will be ready to autoload after successful update.
Thanks for your responses and for your answer #Thomas. I followed your response and was able to add the correct namespace in my controller.
I have a problem. I am building an administration panel for my application and decided, because of the many functions, to use a RESTfull route. Now, because I do not want to jam every function in the same class I also use namespacing and extend my AdminController class.
The problem is, RESTFull works for the functions declared in the AdminController file, but it does not recognize the functions deeper within the namespace. What is the correct way to do this?
This is the code I have right now:
RESTfull Route
Route::controller('admin', 'Admin\AdminController');
AdminController (/controllers/AdminController.php)
namespace Admin;
use View;
class AdminController extends \BaseController {
public function getSales() {
echo"Works";
}
DashboardController (/controllers/admin/DashboardController.php
namespace Admin;
use AdminController;
use View;
class DashboardController extends AdminController {
public function getDashboard() {
echo"Does not work";
}
I can access www.domain.com/admin/sales just fine, but when I access www.domain.com/admin/dashboard it gives me a "Controller method not found" error.
I think you should provide this route manually:
Route::controller('admin/dashboard', 'Admin\DashboardController');
In your code Laravel has no idea that it should use DashboardController and not AdminController for admin/dashboard route