Using Laravel RESTfull with namespacing - php

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

Related

Importing Laravel Avored Package Controller in Laravel Controller

I am trying to extend the package controller in my base laravel controller. Tried importing class using below code which shows error as class not found.
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ProductController as ControllersProductController;
use App\Imports\ProductsImport;
use AvoRed\Framework\AvoRedProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;
use Avored\Framework\Catalog\Controllers\ProductController;
class ProductControllers extends Controller
{
private $avored_product;
public function __construct(ProductController $p) {
$this->avored_product = $p;
}
public function index() {
echo $this->avored_product;
}
Tried multiple options by researching on it couldn't find the same. Request all to please guide me with same.
Could you share with us the exact error message? From your code snippet I can't see which class couldn't be found.
And which Avored package are you referring to? I guess avored-laravel-ecommerce?
If you want to extend the ProductController-from the package, you have properly extend from that controller.
<?php
namespace App\Http\Controllers\Admin;
use Avored\Framework\Catalog\Controllers\ProductController as AvoredProductController;
class ProductControllers extends AvoredProductController
{
public function index() {
// Do your thing in here
}
}
You can now override the controller methods to your liking.

How to use a trait as a route controller method in laravel?

Target [App\Http\Controllers\Traits\FileUploadTrait] is not instantiable.
I get this error when trying to send a file upload to this route:
<?php
namespace App\Http\Controllers\Traits;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
trait FileUploadTrait
{
/**
* File upload trait used in controllers to upload files
*/
public function saveFiles(Request $request)
{
//some file upload code
}
}
in my route:
Route::post('upload/files', ['uses' => 'Traits\FileUploadTrait#saveFiles', 'as' => 'media.upload']);
how to use a trait as a route controller#method?
You can't use a Trait as controller since Trait's are not classes, but actually they "are a mechanism for code reuse in single inheritance languages such as PHP." (See php docs).
A trait is not instantiable, this means that under the hood, Laravel can't do
$controller = new FileUploadTrait() to use it.
To use a trait, you must include it on some class, for example:
class MyController {
use FileUploadTrait;
}
Then you define your routes to use that class that you define.
traits arent callable... which means :) you cannot call them :Dlol, sorry - couldnt help myself... anyway try with something like this :)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
class MahController extends Controller {
use App\Http\Controllers\Traits\FileUploadTrait;
}
Traits cannot be 'instantiated', you add them on objects.

Model Class not found error

Using Laravel 5.4, I am getting this error on view whenever I call my method it shows on screen
(1/1) FatalThrowableError
Class 'App\Models\Chat\User' not found
Hierarchy of my project :
Controllers
- ChatMessageController
Models
-Chat
-message.php
-User.php
and here's my controller class code :
namespace App\Http\Controllers\Chat;
use App\Http\Controllers\Controller;
use App\Models\Chat\Message;
use App\Models\User;
use Illuminate\Http\Request;
class ChatMessageController extends Controller
{
public function index()
{
$messages = message::with(['user'])->latest()->limit(100)->get();
return response()->json($messages,200);
}
}
In message.php, probably you forgot to add: use App\Models\User;.
So, it is trying to find User in the wrong space.

Laravel controllers not working

I'm learning laravel but it isn't working out that well....
I've set my route in routes.php:
Route::get('/','WelcomeController#index');
Then I obviously have made a controller called "WelcomeController" and it looks like this:
<?php
class WelcomeController extends BaseController
{
public function index()
{
return view ('index');
}
}
?>
And then I've made a view called index with just some html text.
But when I go to localhost/public I receive the error:
FatalErrorException in WelcomeController.php line 3:
Class 'BaseController' not found
And when I say:
class WelcomeController extends Illuminate\Routing\Controller
It does not work!
What am I doing wrong.
You should try
use Illuminate\Routing\Controller as BaseController;
at the top of your controller file. That acts as an import
Two suggestions:
Run php composer dump-autoload to make sure the class mappings is fresh.
Add use Controller; in your use block. The modify your controller to extend it. Example:
class WelcomeController extends Controller {...
Controller is an interface in Laravel 4.*
In Laravel 5 use instead:
use App\Http\Controllers\Controller; according to the documentation here: http://laravel.com/docs/5.0/controllers

Yii 2 Namespace missing?

I have a namespace missing problem in Yii 2. I installed the advanced application. I am referencing a backend model from my frontend controller. Below is a code snippet of my backend model, frontend controller and error message.
Error
Unable to find 'backend\models\PaymentsMethod\TermsAndConditions' in file: C:\inetpub\wwwroot\jobmanager/backend/models/PaymentsMethod/TermsAndConditions.php. Namespace missing?
Backend Model
namespace app\models\PaymentsMethod;
use Yii;
class TermsAndConditions extends \yii\db\ActiveRecord
{
Frontend Model
public function actionCreate()
{
$model = new estimate();
$tnc = new \backend\models\PaymentsMethod\TermsAndConditions();
I have resolved my problem. I was trying to access a backend model class from a frontend controller. I resolved this by moving the backend model class to the common folder and from there I can reference it from both the backend and frontend.
Thanks
In your frontend, first include the namespace and then instantiate:
use app\models\PaymentsMethod\TermsAndConditions;
$tnc = new TermsAndConditions();
OR
As alfallouji said you can directly use:
$tnc = new \app\models\PaymentsMethod\TermsAndConditions();
If you are accessing from frontend then use frontend instead of app
i.e
namespace frontend\models\PaymentsMethod;
and if you are accessing from backend then use as below
namespace backend\models\PaymentsMethod;
You defined the model using this namespace app\models\PaymentsMethod and then you are trying to instantiate \backend\models\PaymentsMethod\TermsAndConditions.
You should be doing that in your frontend model :
$tnc = new \app\models\PaymentsMethod\TermsAndConditions();
Namespace declaration statement has to be the very first statement in the script
123456789101112
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\users;
class UserController extends Controller{
public function actionIndex()
{
echo "working on .....";
replace "backend" for "app" only models search
ex: app\models\PaymentsMethod;

Categories