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.
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
I'm trying to create a crud in Symfony 4.4 with bin/console make:crud command but I'm getting the following error
In MakeCrud.php line 103:
Call to a member function getRepositoryClass() on null
this is part of the class I'm trying to use for the crud
<?php
namespace Domain\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
class Segment
{
private $id;
private $uidentifier;
private $name;
I type the class name with the namespasce oto genterate crud but I get the error, what could be wrong?
So I got a little bit curious and confirmed that it is a namespace issue.
I made myself a Domain\Entity\Segment class and then ran
bin/console make:crud 'Domain\Entity\Segment'
And got the same error message.
The make:crud command expects entities to live under App\Entity. Does not seem to be any way to convince it to look under Domain\Entity just for entities.
However the Maker Bundle itself allows you to change the name of the App's root namespace:
# config/packages/maker.yaml Need to add this file
maker:
root_namespace: Domain
# Then run
bin/console make:crud Segment
And it works. The problem is that the root namespace is used for generating all the files so, for example, you end up with a Domain\Controller\SegmentController class which is probably not where you want them to be.
I checked the source and there is no easy way to adjust just the entity namespace. If you plan on using crud quite a bit then you either need to copy your entities to App\Entity or specify the root_namespace and then copy/refactor your generated controllers and forms. Either way it will be a pain.
I suppose you could also fork the maker bundle and edit the source code directly. Might actually be worth the effort if you have a bunch of crud to generate. But that will be left as an exercise.
I had anonymous component resources\views\components\homepage\feedback.blade.php to render feedback on homepage. From the beginning it was just html. Then I decided to connect Class file. I already had another View Class component and I just copied it manually instead of using artisan command.
App\View\Components\Feedback.php
namespace App\View\Components;
use Illuminate\View\Component;
use App\Models\Feedback;
class Feedback extends Component
{
public $feedbacks;
public function __construct()
{
$this->feedbacks = Feedback::wherePublished(true)->take(5);
}
public function render()
{
return view('components.homepage.feedback');
}
}
And then {{ dd($feedbacks) }} in view file gives me error that this variable is not defined.
Undefined variable: feedbacks (View: C:\laragon\www\lara7\resources\views\components\homepage\feedback.blade.php)
If I try to create Test component with artisan command and put this code inside it works, but then I cannot rename it back to Feedback class. It gives me error
Symfony\Component\ErrorHandler\Error\FatalError
Cannot declare class App\View\Components\Feedback because the name is already in use
But old class already deleted, so I cannot understand what is wrong.
It seems like there is some hidden link between View Class and Blade components, which needs to be erased. But where is this link located?
When switching component type from anonymous to class and back, you have to clear compiled view files:
php artisan view:clear
That's because Laravel incorporate specific component type invocation into the compiled view code.
I found problem.
I got $feedbacks is undefined, because my anonymous component without variables initially was located in resources\views\components\homepage\feedback.blade.php and when I decide to create View Class for this component there was no link established.
Laravel creates automatic link between feedback.blade.php and app\View\FeedbackComponent.php only when blade file located directly in resources\views\components folder. And my component was in subfolder.
So laravel tried to render resources\views\components\homepage\feedback.blade.php with $feedback variable inside and it cannot find where $feedback is defined.
So I just manually register FeedbacksComponent class like that in appservice provider boot method
Blade::component('homepage-feedbacks', FeedbacksComponent::class);
and then use <x-homepage-feedbacks/> to render it
I would say documentation is not very clear. It says that outside of components folder automatic discovery is not working.
But it doesn't say that inside components subfolder automatic discovery is not working.
In Laravel 8 you can use and no need to declare the component
<x-homepage.feedback />
I think you're right I have been having the same issue and and I've been really struggling with it. Finally I found a workaround which is if you change the file name it works so I think it's a problem with the laravel framework and I think they need to address this issue
I've created a helper file using the following method:
Created file in app/Http/helpers.php
Added file path in composer.json
Run this command: composer dumpautoload
Everything worked well but I want to use my models here so that I don't have to write code for getting data from database every time, but it's giving an error that model class not found.
function productImagePath($image_name) {
$generalSettings = \GeneralSettingsModel::GetGeneralSettings();
return $generalSettings;
}
My requirement is that I want to call some model's functions to get frontend (header/footer) design settings from database. So I've to write that code again and again in each controller. I'm very new in Laravel so if it's not a good approach to use helpers for such things, please guide me how it's possible then.
As your GeneralSettingModel class is in the App namespace you will need to include that:
function productImagePath($image_name)
{
$generalSettings = App\GeneralSettingsModel::GetGeneralSettings();
return $generalSettings;
}
Hope this helps!
I trying to create new migration with Symfony:
$ php app/console doctrine:migrations:generate
Generated new migration class to "/path/to/project/app/DoctrineMigrations/Version20100621140655.php"
But I want create migration with name Version20100621140655_MyName.php instead of Version20100621140655.php. How can I do this?
EDIT
Linked theme: https://github.com/doctrine/migrations/issues/487
Since I came here by googling an none of the already posted answers gave me the direct solution, I figured it out by checking out GitHub issues and the Doctrine documentation.
I have tried the suggested answer from COil but my migrations failed at the post check, due to that the versions field in the migration_versions table is set to varchar(14) by default. Since the name will most probably become longer than that, you'd run into this error most of the time.
The solution is to adjust the doctrine_migrations.yaml file and add column_length: YOUR_PREFERRED_LENGTH (in my case 60).
doctrine_migrations:
...
column_length: 60
Now you can always manually rename your migration file and adjust the class name accordingly inside that file, to for example Version20100621140655_MyName.php.
class Version20100621140655_MyName extends AbstractMigration
{
}
There is no way to do this beyond creating your own command.
The class name is Version<version> as your can see in the code and the file name, obviously, needs to be the same as that.
The version is created here using $version = date('YmdHis').
For the benefit (a suffix to the file and class name) I doubt the work would be worth it to add this capability. You can always change the namespace easily enough though if you wanted to keep it in your app namespace using...
app/config/config_dev.yml
doctrine_migrations:
...
namespace: Your\Migrations\Namespace
Rename the file and change the name of the class:
class Version20100621140655_MyName extends AbstractMigration
{
//
EDIT: doesn't seem to work, Doctrine is reporting a new migration but can't execute it...