How to use methods from one controller in another - php

I have installed waavi package for manipulation of translation files. I need to use methods from it's controller to mine? I tried something like this but it doesn't work
LanguageRepository::findByLocale(1);
This is what I am using in beginning of my controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Waavi\Translation\Repositories\LanguageRepository;
use Waavi\Translation\Repositories\TranslationRepository;
use Illuminate\Foundation\Application;

If you have successfully done all the steps in here, you should be able to access to LanguageRepository using depedency injection(" It is recommended that you instantiate this class through Dependency Injection")
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Waavi\Translation\Repositories\LanguageRepository;
class DefaultController extends Controller
{
private $language_repository;
function __construct(LanguageRepository $language_repository)
{
$this->language_repository = $language_repository;
}
public function index()
{
dd($this->language_repository->findByLocale("en"));
}
}
Note: you need pass language string instead of id to findByLocale method. see line 97

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.

Can't find new controller created

I everyone I've created a new Controler inside my App\Controllers\Admin folder and I already have two files called AdminInquiriesController and AdminUsersController.
When I run my app it says that
Class App\Http\Controllers\AdminNewsController does not exist
I don't undersantd. In all my 3 files inside this folder I'm using the namespace namespace
App\Http\Controllers
if it's working for the others why is not working for this?
<?php
namespace App\Http\Controllers;
use App\Manager\InquiryManager;
use Auth;
use Illuminate\Http\Request;
use function GuzzleHttp\json_decode;
use App\Model\InquiryStatus;
use Carbon\Carbon;
use App\Manager\UserManager;
class AdminInquiryController extends Controller {
<?php
namespace App\Http\Controllers ;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Carbon\Carbon;
use App\Manager\NewsManager;
class AdminNewsController extends Controller {
In my route file..
// inquiries
Route::get('/admin/inquiries', 'AdminInquiryController#search');
// news
Route::get('/admin/news', 'AdminNewsController#search');
Route::post('/admin/news/new', 'AdminNewsController#create');
I know this is so silly but I'm not understanding what's happening...
You should to define namespace in your controller file;
namespace App\Http\Controllers\Admin ;
Also change your web.php route file to
Route::get('/admin/news', 'Admin\AdminNewsController#search');
Or you can define namespace in routes group by
Route::namespace('Admin')->group(function () {
Route::get('/admin/news', 'AdminNewsController#search');
}
Also you have to put
use App\Http\Controllers\Controller;
In your controllers files in Admin folder
Try to add a second forward slash when writing model location
something like this
--model=App\\Models\\ModelName

laravel5.2 Auth::guest() returns error "Class 'App\Http\Controllers\Auth' not found"

I am trying to use the inbuilt Auth for login in laravel and register. These automatically generated pages work so well, now I use Auth::guest() to check if the user is authorized to return a view: index else to login page.
But it shows:
Class 'App\Http\Controllers\Auth' not found".
Here's the code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
class StepsController extends Controller
{
public function step1()
{
if (Auth::guest())
{
return redirect('login');
}else {
return view('index');
}
}
}
You need to "import" the definition of the facade of Auth and remove the use App\Http\Controllers\Auth because does not exists.
Just add at the top (just before the class declaration):
use Auth;
And remove the other:
Having this:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
class StepsController extends Controller
{
....
Hope it helps.
PS: If you want to learn Laravel with a good guide, step-by-step, please check here: Learn Laravel

Laravel 5 registering a controller to map all methods

I am new to Laravel 5 coming from CodeIgniter background. I have habit to not play with routes.php. CodeIgniter automatically maps methods like controllerName/MethodName. But in Laravel 5 I am trying to do same by registering a controlller by writing this at top of app/http/sroutes.php:
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
When I run php artisan route:list it show that controller is registered. But when I see URL /public/admin/user/addRole it show addRole method not exist while I have created a method in AdminUserController.
Admin/AdminUserController.php
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getaddRole(){
echo "adding Roles";
}
}
Routes.php
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getAddRole(){
echo "adding Roles";
}
}
NB: Notice getAddRole() not getaddRole(), use camelCase
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI like this:
public/admin/user/add-role
It's hard to tell because I don't see your controller code but I assume you missed adding a HTTP verb to the method name. Like:
public function getAddRole(){
// ...
}
If you want the method to match any request method, use any:
public function anyAddRole(){
// ...
}

How do I call a model in Laravel 5?

I'm trying to get the hang of Laravel 5 and have a question which is probably very simple to solve but I'm struggling.
I have a controller named TestController and it resides in \app\Http\Controllers
This is the controller:
<?php
namespace App\Http\Controllers;
class TestController extends Controller {
public function test()
{
$diamonds = diamonds::find(1);
var_dump($diamonds);
}
}
Then I have a model I created that resides in /app:
<?php
namespace App;
class diamonds extends Model {
}
Put all other mistakes aside which I'm sure there are, my problem is that laravel throws an error:
FatalErrorException in TestController.php line 10: Class
'App\Http\Controllers\diamonds' not found
So, how do I get the controller to understand I'm pointing to a model and not to a controller?
Thanks in advance...
You have to import your model in the Controller by using namespaces.
E.g.
use App\Customer;
class DashboardController extends Controller {
public function index() {
$customers = Customer::all();
return view('my/customer/template')->with('customers', $customers);
}
}
In your case, you could use the model directly App\diamonds::find(1); or import it first with use App\diamonds; and use it like you already did.
Further, it is recommended to use UpperCamelCase class names. So Diamonds instead of diamonds. You also could use dd() (dump and die) instead of var_dump to see a nicely formatted dump of your variable.
//Your model file
<?php
namespace App\Models;
class diamonds extends Model {
}
// And in your controller
<?php
namespace App\Http\Controllers;
use App\Models\
class TestController extends Controller {
public function test()
{
$diamonds = diamonds::find(1);
var_dump($diamonds);
}
}
Try adding the following lines above your class decleration in your controller file...
use App\Diamonds;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
(this assumes your model file is called Diamonds.php)
Ultimately, there were a few issues here and all of you helped, however I was missing the following on the model page:
use Illuminate\Database\Eloquent\Model;

Categories