Jump to content

Recommended Posts

 Hi,
In laravel 5.4 I want to make common control and all my controls inhereted from it, like
file app/Http/Controllers/MyAppController.php with :

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use App\Carbon;
use App\Settings;
use Route;
class MyAppController extends Controller
{

and defining app/Http/Controllers/ShippingClassController.php :

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\MyAppController;
use Auth;
use Illuminate\Http\Request;  IF THIS LINE IS COMMENTED I GOT ERROR : Class App\Http\Controllers\Request does not exist
use App\ShippingClass;
use Carbon\Carbon;
class ShippingClassController extends MyAppController
{public function __construct()
{...

Actually I hoped that in app/Http/Controllers/ShippingClassController.php I have not to include

use Illuminate\Http\Request;

class 1 more time, but I failed.

If there is a way to make it ?

 

Thanks! 

Link to comment
https://forums.phpfreaks.com/topic/303788-defined-class-twice-in-controlds/
Share on other sites

uses are not defining classes. They let you use a shorter name for a class that's referenced in the code.

If you just write "Request" then PHP will try to find a Request class in the current namespace (App\Http\Controllers). It doesn't exist. You need to tell it that the real class is Illuminate\Http\Request, and you do that either by using the full name of the class everywhere (awkward) or by continuing to write "Request" and including a use statement.

 

uses are per-file. The fact that you have one in MyAppController.php has absolutely no bearing on what happens in ShippingClassController.php.

 Thank you for the explanations!
Let me similar question.
I made class of my function located at app/library/appFuncs.php :

<?php namespace App\library {

use Barryvdh\Debugbar\Facade as Debugbar;
class appFuncs {
private static $m_concat_str_max_length = 30;
public static function someFunction($s, $src= '')
{
...

I wrote it in composer.json

..."autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"appFuncs": ["app/library/appFuncs"]

},...

and run command in console :

# composer update

But anyway to call methods of this class I need to insert refs to it

use App\library\appFuncs;

In control or in view where I want to ref it.

Which way is right? 

Namespaces are like directories: if there is a class in a namespace that you want to reference, and the code you're writing is not already in the same namespace, then you need to write the full path to the class or to include a use statement that has the full path.

 

So the question is: are your controllers and views in the same namespace as appFuncs?

I suppose no,

Then there's your answer: add use statements to files that need appFuncs, because the alternative of writing the full name is a bother.

 

but which is the best way to make them under 1 namespace ?

Don't. Separating classes from each other is good. Putting classes in the appropriate namespaces is good. Referencing classes across namespaces is perfectly acceptable.

I think you are right,

That is just a habit from codeigniter, when in file application/config/autoload.php I added line:

$autoload['libraries'] = array('appUtils' , 'appCart', 'ion_auth', 'appTwig' );

 

in cases if some libs were used in many parts of app...

If in laravel something like this?

Putting "appUtils" in a string could cause issues, but I don't think that's how Laravel does it?

 

Are you having a problem with autoloading?

 

Yes, I wrote about it above, as I added into composer.json lines:

"autoload": {
  "classmap": [
  "database"
],
"psr-4": {
  "App\\": "app/"
},
  "appFuncs": ["app/library/appFuncs"]

},

and running 

# composer update

I will not have to add in header of any Control/view line :

use App\library\appFuncs;
 
Is my syntax ok for laravel 5.4 ?
Edited by mstdmstdd

Okay, you seem to be fundamentally misunderstanding what the use statement does, and likely don't quite grasp what namespaces are either. Still. Please spend time reading how it works.

 

Here are a couple links to get started:

 

http://php.net/manual/en/language.namespaces.php

http://stackoverflow.com/questions/10542012/php-namespaces-and-use

 

Afterwards you should be able to understand why setting up Laravel for autoloading has nothing to do with whether you have use statements in your code.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.