I am trying to use this package within my Laravel project:
https://github.com/bkuhl/simple-ups
I am however struggling to use it within my controller as it has an hyphen within its path.
I have tried doing:
use Bkuhl\simple-ups\src\SimpleUPS\UPS.php
but it does not seem work. Does anyone have a guide as to how I can use it within my controller?
use SimpleUPS\UPS;
You can view the namespace of the class here
https://github.com/bkuhl/simple-ups/blob/master/src/SimpleUPS/UPS.php
Related
I've performed a long-overdue update on a Laravel project from v5.7 to v9 and ran into a Target class does not exist error. I found this guide and used the first method to resolve the error (adding the namespace into the RoutesServiceProvider.php boot function). This resolved that error but now, everything is giving me Class "App\Whatever" not found.
I did notice that models are now stored in a Models directory within the app directory rather than directly in app, so have moved them to Models. I figured that might be breaking my use App\Whatever; lines at the top of my controllers, so I've tried use App\Models\Whatever and also use app\Models\Whatever (since the "a" is lowercase in the directory name) but no effect.
I should note I don't really have a good grasp of namespaces, MVC frameworks etc. so ELI5 etc :-)
Some of my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Thing;
use App\AnotherThing;
...
public function thing_summary($id) // show the summary view of the thing
{
if(Auth::check()) {
$thing = Thing::find($id);
...
Laravel 7/8/9 sticks with strict namespacing. When you move the models to a new directory, you need to update the namespace in the models themselves (if specified at all) and any file that has a use for the model. If the models move from app/ to app/models, the namespace must be changed to App/Models
Hi i'm newbie in Laravel just practicing with some tutorial videos and i'm wondering if it is possible to configure the brackets position when i use the artisan commands.
For example when i create new controller using artisan make:controller it makes
class Controller extends Controller
{
//
}
What i want is the brackets places right next to the class declaration not in the new line like above. To be more specific
Class Controller extends Controller{
//
}
This is what i want. Is it possible to configure the brackets position?
I've googled a lot and read documentation but couldn't find any of information about it.
--Edited--
Thanks to you guys i know it's not Laravel problem and it can be configured in code editor.
I'm using vs code and have installed several php formatter but it seems all of them follow the PSR-2 styles.
I found JS configuration for its brackets position but couldn't find for PHP
i know it's not even problem i just don't like this format
As mentioned in the comments, the stubs that Laravel generates follow PSR-2 so making this change would break that.
To override the controller code that Laravel generates with make:controller you're going to have to override the ControllerMakeCommand and copy the default stubs you want to edit.
The stubs for this command have changed quite a bit with the different versions of Laravel so it will require a bit of copy and paste to ensure it works correctly.
Extend the ControllerMakeCommand
Inside your app/Console/Commands directory create ControllerMakeCommand.php and add the following:
<?php
namespace App\Console\Commands;
use Illuminate\Routing\Console\ControllerMakeCommand as BaseCommand;
class ControllerMakeCommand extends BaseCommand
{
//
}
Add the getStub method
Find the getStub() method in the Illuminate\Routing\Console\ControllerMakeCommand and copy that into your newly created ControllerMakeCommand.
Copy the default stubs
Copy
vendor/laravel/framework/src/Illuminate/Routing/Console/stubs
to
App/Console/Commands/stubs
(you don't have to copy all of the files inside it, just the controller.* that you want to override).
Change the formatting of the files as you see fit.
Laravel <=5.4
If you're using Laravel <=5.4 commands are not automatically loaded for you so you're going to have to tell Laravel that you want to use this command.
In your App\Console\Kernel.php add your new class to the $commands array e.g.:
protected $commands = [
\App\Console\Commands\ControllerMakeCommand::class
];
NB Obviously, feel free to store the stubs wherever you want just make sure you update the paths in your new ControllerMakeCommand.
I am using laravel 5.1. I want to use XML parser and I have searched and found Orchestra as mostly being used. So I have gone thorough all the steps given at documentation to install and configure. I have added Orchestra\Parser\XmlServiceProvider::class in providers section of config/app.php and 'XmlParser' => Orchestra\Parser\Xml\Facade::class in aliases section.
Now in my controller, I have added its name space like use Orchestra\Parser\Xml\Facade; at the top of my controller. But when i try to use its function in my action, like
$xml = XmlParser::load($xml_document);
It generates error stating,
Class 'App\Http\Controllers\XmlParser' not found
So I want to know is there any other way in Laravel 5.1 to use the packages and I am doing some thing wrong with Orchestra if some one has used it.
Since the documentation already describe registration of the facade alias:
'XmlParser' => Orchestra\Parser\Xml\Facade::class,
You can either use \XmlParser::load(), or import the alias.
use XmlParser;
or import the full namespace.
use Orchestra\Parser\Xml\Facade as XmlParser;
It looks as though it's searching inside the controllers for it..
Class 'App\Http\Controllers\XmlParser' not found
Therefore:
$xml = XmlParser::load($xml_document);
Needs to be:
$xml = \XmlParser::load($xml_document);
Should resolve this problem
In Laravel 5.1 the controller is in a namespace. The XmlParser is in an other namespace. You need to include that namespace in your controller.
<?php
namespace Orchestra\Parser\Xml; // Maybe this one is different
class Controller...
You can also add a \ to make it work.
$xml = XmlParser::load($xml_document);
What I want to do is to use different namespaces in a controller. I have a tree scheme like this:
app/
app/controllers/
app/modules/
app/modules/modulename/
app/modules/modulename/controllers/
app/modules/modulename/controllers/modulecontroller.php
app/modules/modulename/models/
app/modules/modulename/models/modulemodel.php
What I want to do is to call a model from a controller in app/controllers/ folder. Therefore I am supposed to add namespace as follows:
use App\Modules\Facebook\Controllers\Facebook;
The problem is that when I add a namespace and use App::() function at the sametime, I get the following error:
Class 'App\Modules\Modulename\Controllers\App' not found
I think it is looking the App::() function in module folder. How can I solve this problem?
if you use App inside your App\Modules\Facebook\Controllers namespace, it will be interpreted as App\Modules\Facebook\Controllers\Facebook\App class.
since you don't want to have the previous namespace, you use a \ before App like:
\App::()
or put a use statement of top the class like use App;
You probably are creating an unusual namspace scheme. It appears you are namespacing every class from your module differently. You should namespace your code within your module only, like so:
// Adding Onur to the namespace prevents any future namespace collisions.
<?php namespace Onur\Facebook;
After creating your namespace you should add all classes that are outside of your namespace that you want to use as followed.
use Eloquent, Input, Validate, Etc;
This prevents you from adding a \ in front of every class instance, making your code hard maintain and prone to errors. It also gives you a good overview on all the classes you are using in the current class.
if you say
use App\Modules\Facebook\Controllers\Facebook;
then you are supposed to use Facebook instead of App... Or donĀ“t I understand your problem correctly?
if you say
use App\Modules\Facebook\Controllers\Facebook as FacebookController;
the you can use FacebookController in your file
if you need Access to the root App, you need to to root it using a leading \
\App::make()
Is there a way to put your own code into namespaces using cakephp? The following very simple controller class works fine.
class Customer extends \AppModel {
var $name = 'Customer';
}
However, if I add
namespace foo\bar;
cakephp can't find the controller anymore. Is there some way to tell cake in which namespace it should look for controllers?
I am using cakephp 1.3 and php 5.3.
I don't think there is. CakePHP looks for classes like PostsController or BlogController, not foo\bar\PostsController. Maybe you can tell CakePHP in what folder to look for those classes (probably), but then it will still be looking for unnamepsaced class names.
Why would you want this in a framework that doesn't use namespaces?
Why not give up the App::import() in cakephp 1.3. Replace it with the include_once().
I got my customize vendor classes defined under a namespace works fine. Just to prevent the collision of the custom class name with the official one.