Laravel external class files - php

I have a file that i put in app\Classes\myVendor\dev_client_api.php. This file has a class in it:
class someClass{
//stuff
}
I want to use this class in a controller.
In my controller I have done the following:
namespace App\Classes\myVendor;
use dev_client_api;
class myController extends Controller
{
///stuff
public function processData(Request $request){
$client = new someClass($vars);
}
}
When i execute this page I get:
Class 'App\Classes\myVendor\Controller' not found
I have to admit I am not sure what exactly I am doing. Any help would be great.

I assume your Controllers are in Laravel's default App\Http\Controller directory.
namespace App\Classes\myVendor;
class someClass {
//stuff
}
namespace App\Http\Controllers;
use App\Classes\myVendor\someClass;
class myController extends Controller
{
///stuff
public function processData(Request $request){
$client = new someClass($vars);
}
}

Related

How to call Controller Method in a Controller in a different namespace in laravel

I want to call a function of CommonController in PostsController.
PostsController is in Posting namespace
PostController: App\Http\Controllers\Posting\PostsController
CommonController: App\Http\Controllers\CommonController
I tried this code but it did not work in PostingController, it is a small code from my PostingController
namespace App\Http\Controllers\Employer;
use App\Http\Controllers\CommonController;
class PostsController extends Controller
{
public function myFunction(Request $request, $id){
$commonControllerObj = new CommonContoller;
$result = $commonControllerObj->commonCallingFunction($id);
}
}
but it did not work, its giving error
Class 'App\Http\Controllers\Posting\CommonContoller' not found
The first your namespace is wrong
namespace App\Http\Controllers\Posting;
The second you can call another Controller like this
app('App\Http\Controllers\CommonController')->commonCallingFunction();
This will work, but this is bad in terms of code organisation
You can extends Controller like this
use App\Http\Controllers\CommonContoller;
class PostsController extends CommonContoller
{
public function myFunction(Request $request, $id){
$result = $this->commonCallingFunction($id);
}
}

Accessing model methods from controller class Laravel

I am new to Laravel, I am trying things around while going through a tutorial. This is where I am facing an unexpected behaviour.
I have a model tweet and a controller named tweetsController; when I call tweet::find() or any similar method I found this:
FatalErrorException in tweetsController.php line 13:
Class 'App\Http\Controllers\tweet' not found
I have also tried App\tweet::find().
Everything seems fine through tinker.
Please explain.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class tweetsController extends Controller
{
public function show(){
$data = tweet::first()->tweetBody;
return view('tweets.list',['passedData'=> $data]);
}
public function delete($id){
return "here we dele the tweet ".$id;
}
public function add(){
return "i add your tweet to database then show you all the tweets";
}
}
tweet.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tweet extends Model
{
protected $fillable = array(
'tweetHead',
'tweetBody'
);
}
?>
A few options the maybe generating this error:
The model/controller namespace is incorrect;
The name of the file and the class name for the model needs to be "Tweet" with the first letter in uppercase;
If you set the right namespace on the model "Tweet.php" and import that on your "TweetController.php"
I hope that helps :)
UPDATE:
In the TweetController.php add this
use App\Tweet;
Before the class declaration, like this
use App\Tweet;
class tweetsController extends Controller
{
And remember to change the controller name in the class declaration like this
class TweetsController extends Controller
{
And the controller filename will become "TweetsController.php"
The Model also has to be named "Tweet" and not "tweet" in the class declaration and the filename
class tweet extends Model
will become
class Tweet extends Model
and the file will be named "Tweet.php"
and everytime you need to call the model you will do this
public function show(){
$data = App\Tweet::first()->tweetBody;
return view('tweets.list',['passedData'=> $data]);
}
Add use Add\User below namespace in your controller

How to call a model function inside the controller in Laravel 5

I have been facing a problem of not able to use the model inside the controller in the new laravel framework version 5. i created the model using the artisan command
"php artisan make:model Authentication" and it created the model successfully inside the app folder, after that i have created a small function test in it, and my model code looks like this.
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
echo "This is a test function";
}
}
Now i have no idea, that how shall i call the function test() of model to my controller , Any help would be appreciated, Thanks in advance.
A quick and dirty way to run that function and see the output would be to edit app\Http\routes.php and add:
use App\Authentication;
Route::get('authentication/test', function(){
$auth = new Authentication();
return $auth->test();
});
Then visit your site and go to this path: /authentication/test
The first argument to Route::get() sets the path and the second argument says what to do when that path is called.
If you wanted to take this further, I would recommend creating a controller and replacing that anonymous function with a reference to a method on the controller. In this case, you would change app\Http\Routes.php by instead adding:
Route::get('authentication/test', 'AuthenticationController#test');
And then use artisan to make a controller called AuthenticationController or create app\Http\Controllers\AuthenticationController.php and edit it like so:
<?php namespace App\Http\Controllers;
use App\Authentication;
class AuthenticationController extends Controller {
public function test()
{
$auth = new Authentication();
return $auth->test();
}
}
Again, you can see the results by going to /authentication/test on your Laravel site.
Use scope before method name
<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class Mainmenu extends Model
{
public function scopeLeftmenu() {
return DB::table('mainmenus')->where(['menu_type'=>'leftmenu', menu_publish'=>1])->orderBy('menu_sort', 'ASC')->get();
}
}
above code i tried to access certain purpose to call databse of left menu
than we can easy call it in Controller
<?php
Mainmenu::Leftmenu();
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function scopeTest(){
echo "This is a test function";
}
}
Just prefix test() with scope. This will become scopeTest().
Now you can call it from anywhere like Authentication::Test().
For me the fix was to set the function as static:
public static function test() {..}
And then call it in the controller directly:
Authentication::test()
You can call your model function in controller like
$value = Authentication::test();
var_dump($value);
simply you can make it static
public static function test(){
....
}
then you can call it like that
Authentication::test();
1) First, make sure your Model is inside a Models Folder
2) Then supposing you have a model called Property inside which you have a method called returnCountries.
public function returnCountries(){
$countries = Property::returnCountries();
}
of course, in your case, replace Property by the name of your Model, and returnCountries by the name if your function, which is Test
and in the Model you write that function requesting the countries
so in your Model, place a:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return $test = "This is a test function";
}
}
and this is what your Controller will be getting
You should create an object of the model in your controller function then you can model functions inside your controller as:
In Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return "This is a test function"; // you should return response of model function not echo on function calling.
}
}
In Controller:
namespace App\Http\Controllers;
class TestController extends Controller
{
// this variable is used to store authenticationModel object
protected $authenticationModel;
public function __construct(Request $request)
{
parent::__construct($request);
$this->authenticationModel= new \App\Authentication();
}
public function demo(){
echo $this->authenticationModel->test();
}
}
Output:
This is a test function

When exactly do you need to import in PHP?

When autoloading classes, the following runs without a problem:
<?php
namespace App\Resources;
class Home extends Controller {
public function index() {
echo 'home/index';
}
}
How does this work? I never imported the Controller class:
<?php
namespace App\Resources;
use App\Resources\Controller;
class Home extends Controller {
public function index() {
echo 'home/index';
}
}
If you use non-qualified class name (without the namespace), PHP assumes you mean the current namespace. The code above works because both Home and Controller are in the same namespace App\Resources.

Laravel, namespaces and PSR-4

I'm trying to set up PSR-4 within a new Laravel 4 application, but I'm getting some troubles achieving what I want when it comes to build controllers.
Here's what I have now :
namespace MyApp\Controllers\Domain;
class DomainController extends \BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = \View::make('domain.home');
}
}
I'm not so fond of using \View, \Config, \Whatever to use Laravel's classes. So I was wondering if I could put a use Illuminate\View; to be able to use View::make without putting a \.
Unfortunately, while doing this, I'm getting the following error : Class 'Illuminate\View' not found.
Could somebody help with this please ?
The problem in your case is that View is not located in Illuminate namespace but in Illuminate\View namespace, so correct import would be not:
use Illuminate\View;
but
use Illuminate\View\View;
You can look at http://laravel.com/api/4.2/ to find out which namespace is correct for class you want to use
Assuming BaseController.php has a namespace of MyApp\Controllers\Domain
namespace MyApp\Controllers\Domain;
use View;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home');
}
}
If BaseController.php has other namespace, i.e MyApp\Controllers
namespace MyApp\Controllers\Domain;
use MyApp\Controllers\BaseController;
use View;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home');
}
}
If, for instance, you controller needs to use another base class from Laravel, lets say Config.
namespace MyApp\Controllers\Domain;
use MyApp\Controllers\BaseController;
use View;
use Config;
class DomainController extends BaseController {
public $layout = 'layouts.default';
public function home() {
$this->layout->content = View::make('domain.home')->withName(Config::get('site.name'));
}
}
The use of View::make() takes advantage of the Laravel facades. To properly reference the facade, instead of directly referencing the class that gets resolved out of the iOC container, I would use the following:
use Illuminate\Support\Facades\View;
This will reference the View facade that is being used when calling View::make()

Categories