When exactly do you need to import in PHP? - 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.

Related

Laravel external class files

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);
}
}

laravel is giving error on custom controller used in routes

I have a simple in routes/web.php file
Route::get(Config::get('constants.ADMIN_PATH') . '/categories', 'AdminControllers\AdminPagesController#index');
I have made a folder AdminControllers and inside that there is a controller named AdminPagesController but i am getting error as
Class App\Http\Controllers\AdminControllers\AdminPagesController does not exist
Whereas i looked into the same folder and class exist. Here is my class code
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AdminPagesController extends Controller
{
public function __construct() {
}
public function index () {
return "hello";
}
}
Change you namespace to
namespace App\Http\Controllers\AdminControllers;
Laravel will resolve controllers based on your name spacing, not on your directory structure.
You should specify the namespace correctly, change it to:
namespace App\Http\Controllers\AdminControllers; // <------- correct this namespace
use Illuminate\Http\Request;
class AdminPagesController extends Controller
{
public function __construct() {
}
public function index () {
return "hello";
}
}
Hope this helps!
If you choose to nest your controllers deeper into the **
App\Http\Controllers
** directory, use the specific class name relative to the
App\Http\Controllers
root namespace.
namespace App\Http\Controllers\AdminControllers;

Single page controller not working

So I have created a single page as subpage at:
/application/single_pages/leden/mijnaccount.php
Added it at the single pages list in dashboard.
The page is working fine.
But when I add a controller at:
/application/controllers/single_page/leden/mijnaccount.php
With the following contents to test:
<?php
namespace Application\Controller\SinglePage;
use Concrete\Core\Page\Controller\PageController;
class Mijnaccount extends PageController
{
public function on_start()
{
exit('Started');
}
public function view()
{
exit('View');
}
public function on_before_render()
{
exit('Before render');
}
}
None of those exit() functions get called. What am I doing wrong?
The solution seems to be to add the subfolder to the namespace:
namespace Application\Controller\SinglePage;
Becomes:
namespace Application\Controller\SinglePage\Leden;

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

Yii 2 - using custom helpers

I'm trying to use a custom helper class which i create under frontend/components/Helper (Helper.php)
The content of that file is something like:
<?php
namespace frontend\components\Helper;
class Helper {
public static function helperGreetings() {
echo("hello helper");
}
}
?>
and on my SiteController.php i have the following:
use frontend\components\Helper;
class SiteController extends Controller
{
public function actionIndex()
{
Helper::helperGreetings();
return $this->render('index');
}
}
What should i do to have it working?
BTW, the error i get is Unknown Class – yii\base\UnknownClassException
Unable to find 'frontend\components\Helper' in file: /Users/foo/sites/bar.dev/frontend/components/Helper.php. Namespace missing?
Change the namespace in the Helper class from
namespace frontend\components\Helper;
to
namespace frontend\components;

Categories