mstdmstdd Posted April 23, 2017 Share Posted April 23, 2017 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! Quote Link to comment https://forums.phpfreaks.com/topic/303788-defined-class-twice-in-controlds/ Share on other sites More sharing options...
requinix Posted April 23, 2017 Share Posted April 23, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303788-defined-class-twice-in-controlds/#findComment-1545808 Share on other sites More sharing options...
mstdmstdd Posted April 24, 2017 Author Share Posted April 24, 2017 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? Quote Link to comment https://forums.phpfreaks.com/topic/303788-defined-class-twice-in-controlds/#findComment-1545821 Share on other sites More sharing options...
requinix Posted April 24, 2017 Share Posted April 24, 2017 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? Quote Link to comment https://forums.phpfreaks.com/topic/303788-defined-class-twice-in-controlds/#findComment-1545822 Share on other sites More sharing options...
mstdmstdd Posted April 24, 2017 Author Share Posted April 24, 2017 I suppose no, but which is the best way to make them under 1 namespace ? Quote Link to comment https://forums.phpfreaks.com/topic/303788-defined-class-twice-in-controlds/#findComment-1545825 Share on other sites More sharing options...
requinix Posted April 24, 2017 Share Posted April 24, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303788-defined-class-twice-in-controlds/#findComment-1545828 Share on other sites More sharing options...
mstdmstdd Posted April 24, 2017 Author Share Posted April 24, 2017 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? Quote Link to comment https://forums.phpfreaks.com/topic/303788-defined-class-twice-in-controlds/#findComment-1545831 Share on other sites More sharing options...
requinix Posted April 24, 2017 Share Posted April 24, 2017 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? Quote Link to comment https://forums.phpfreaks.com/topic/303788-defined-class-twice-in-controlds/#findComment-1545834 Share on other sites More sharing options...
mstdmstdd Posted April 25, 2017 Author Share Posted April 25, 2017 (edited) 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 April 25, 2017 by mstdmstdd Quote Link to comment https://forums.phpfreaks.com/topic/303788-defined-class-twice-in-controlds/#findComment-1545869 Share on other sites More sharing options...
requinix Posted April 25, 2017 Share Posted April 25, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303788-defined-class-twice-in-controlds/#findComment-1545870 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.