Symfony and Swift Mailer - swiftmailer

https://swiftmailer.symfony.com/docs/introduction.html
Documents are lacking some information, like all symfony documents are but my problem at moment is that how to instantiate the new Swift_SmtpTransport()?
I cannot find namespace for it. If i do like the document says, i get this error -> Attempted to load class "Swift_SmtpTransport" from namespace App\Controller.
plus, documents don't say how to instantiate Swift_Message class either.
Trying container hasn't worked for me, like so -> $this->container->get

I think the answer is here if you want to send a message -> Attempted to call an undefined method named "newInstance" of class "Swift_Message"
I quote :
Now it is:
$message = \Swift_Message::newInstance()->setSubject('Hello Email')
instead of
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')

Related

Target class [Agent\App\Http\Controllers\PropertyController] does not exist when I put it inside the route group

I have this route in web.php
Route::group(['prefix'=>'agent','namespace'=>'Agent','middleware'=>
['auth','agent'],'as'=>'agent.'], function()
{
Route::get('/dashboard',[AgentController::class, 'index'])->name('dashboard');
Route::resource('/properties', PropertyController::class);
});
When I run the command below,
php artisan route:list
I got this error:
Illuminate\Contracts\Container\BindingResolutionException
Target class [Agent\App\Http\Controllers\PropertyController] does not
exist.
at
C:\xampp\htdocs\sweethomeFinal\vendor\laravel\framework\src\Illuminate\Container\Container.php:879
875▕
876▕ try {
877▕ $reflector = new ReflectionClass($concrete);
878▕ } catch (ReflectionException $e) {
879▕ throw new BindingResolutionException("Target class [$concrete] does
not exist.", 0, $e);
880▕ }
881▕
882▕ // If the type is not instantiable, the developer is attempting to resolve
883▕ // an abstract type such as an Interface or Abstract Class and
there is
1 [internal]:0
Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console{closure}(Object(Illuminate\Routing\Route))
2
C:\xampp\htdocs\sweethomeFinal\vendor\laravel\framework\src\Illuminate\Container\Container.php:877
ReflectionException::("Class Agent\App\Http\Controllers\PropertyController does not exist")
But when I put the "Route::resource('/properties', PropertyController::class);" outside the auth
Route::group(['prefix'=>'agent','namespace'=>'Agent','middleware'=>
['auth','agent'],'as'=>'agent.'], function()
{
Route::get('/dashboard',[AgentController::class, 'index'])->name('dashboard');
});
Route::resource('/properties', PropertyController::class);
It just shows all the route lists. But I wanted to put it inside the auth, may I know what is wrong?
Group namespaces made sense pre-Laravel 8, but now with the suggested way of defining routes as Controller::class the prefixes are basically useless.
Routing before Laravel 8
Before v8, Laravel used a default prefix defined in RouteServiceProvider of App\Http\Controllers\. This meant that you only needed to provide the last part - MyController and it was automatically built out to the fully qualified class name (App\Http\Controllers\MyController).
Routing beginning in v8
In v8, the default controller path was removed ($namespace = null), meaning you have to provide the fully qualified class name yourself, or add the prefix back to the service provider. The most efficient way to do this is using the ::class syntax which returns the required name. This method of providing the class name is also more IDE friendly, which was one of the main reasons for the switch.
The problem with route group namespaces
In the older way of building out the controller class name, the group namespaces were useful for sub folders in your controllers folder.
The path would get built out like:
{default_prefix} + {group_namespace} + {controller name}
Yielding:
App\Http\Controllers\ + Agent\ + PropertyController.
This is actually still how it works in version 8; however, you're providing the values in a different way:
(null) + Agent + App\Http\Controllers\PropertyController, which doesn't make the right path.
Summary
When using the ::class syntax for Laravel routes, group level namespace prefixes really don't make sense to use anymore.
If you browse the versions of the Laravel documentation, you'll also notice that the usage of group namespaces present in version 7 is not included in the version 8 docs, because even though it still "works", it's not very useful.
when you set namespace for a route group, all the routes in this group take that namespace as prefex for it's name.
remove ,'namespace'=>'Agent' form the group definition, it should solve it.
see laravel doc for more details.
your are using laravel 8. add the below line on your web.php .
use App\Http\Controllers\yourcontrollername;

Laravel not finding class

I have a project I'm working on right now, in which I created a database to hold some info. I've already created a view and a controller where I can add data to the DB and it works. But for some reason when I try to use on a view to list my information it won't work.
Here's my model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Banner extends Model
{
protected $fillable = ['descricption','image'];
}
And these are the first few lines of my view which laravel point to error:
#php
use\app\Banner;
$banner = new Banner;
$banners = Banner::all();
#endphp
Whenever I try to open this view, I get the "Class 'app\Banner' not found" error. What am I doing wrong? Since it worked to add data to the database before, and I did remember of putting the use\app\Banner;
The namespace is App not app. Always act like case sensitivity matters and you won't have these problems.
use App\Banner;
Side Note: you can pass this data to your view instead of having to do this query in your view.

presentViewController does not exist

I am building a plugin in NativeScript.
When I try to access "presentViewController" method on rootViewController, I get the error "property presentViewContrller does not exist".
const rvc = UIApplication.sharedApplication.keyWindow.rootViewContrller;
rvc.presentViewContrller(myViewController, true, completion() {});
It suggests to use presentViewContrllerAnimatedCompletion which does not accept my view controller.
Could you please assist what part of my code (or maybe setup!) is wrong?
The right method name is presentViewControllerAnimatedCompletion only.
You should combine the parameter names with method name while marshalling Objective C to JS/TS.
presentViewController:animated:completion

Configure langs in Laravel Translatable BootForms

I'm new to Laravel Translatable BootForms, and I was wondering something.
When I use this code :
{!!
TranslatableBootForm::text('Nom', 'name')
->required()
!!}
The render is as follows :
I don't know where this language list comes from.
I only want to list some languages specified in my database, as I do with this workaround :
#foreach($availableLangs as $availableLang)
{!!
TranslatableBootForm::text('Nom', 'name')
->renderLocale($availableLang['locale'])
!!}
#endforeach
Which gives me this :
My two questions are :
Where does this language list come from ?
How can I replace it by my own language list ?
Answering the first question may lead to an automatic answer for the second, though)
In Laravel, you should always try to read the Service Providers, they provide important clues about the project structures. Let's try to follow the trail of the function calls:
TranslatableBootForm is a facade and it resolves to and instance of translatable-bootform from the Service Container according to this line:
protected static function getFacadeAccessor() { return 'translatable-bootform'; }
Now, in the file TranslatableBootFormsServiceProvider.php we can see that translatable-bootform is an instance of TranslatableBootForm. So when you call TranslatableBootForm::text, you will be using the Facade which resolves to an instance of TranslatableBootForm
Opening the TranslatableBootForm class, we cannot find the text method, so there should be a __call method. The __call method always returns whatever is returned from the method render. So that's where the action is happening.
Reading the code there, you will find that it gets the locales from a method called locales and it will intersect it with the func_get_args() function to get whatever languages you pass to it. So renderLocale or simply render will do the same thing.
The method locales just returns an array which is by default empty in the class. If we return back to the TranslatableBootFormsServiceProvider we will see that there's an important line:
$formBuilder->setLocales($this->getLocales());
Which gets the locales from Translatable\TranslatableWrapper which is just a wrapper around this file in another package: https://github.com/dimsav/laravel-translatable/blob/master/src/Translatable/Translatable.php
Looking at the configuration file in the laravel-translatable package, we can see the languages:
https://github.com/dimsav/laravel-translatable/blob/master/src/config/translatable.php
Solutions
Now, you can simply copy the file translatable.php in your config folder and set your locales.
Or, you create a new service provider MyTranslatableBootFormsServiceProvider
class MyTranslatableBootFormsServiceProvider extends TranslatableBootFormsServiceProvider
{
/**
* Get Translatable's locales.
*
* #return array
*/
protected function getLocales()
{
// You can return a config key
// return config('yourconfig.locales');
// Or directly the array containing the languages
return ['en', 'fr', 'nl'];
}
}
Then, you will use this provider in your config/app.php instead of the original TranslatableBootFormsServiceProvider
Disclaimer:
I didn't try the code, you might have a bug, but you get the idea now how to find your way around Laravel packages.

Laravel 4 route-model binding exceptions doesn't work despite docs and examples

I read a lot about Laravel4 Route-model binding (L4 docs, tutorials, etc.) but still exceptions (i.e. the model is not found) don't work for me
These are my basic files
routes.php:
Route::model('game', 'Game', function(){
// override default 404 behavior if model not found, see Laravel docs
return Redirect::to('/games');
});
...
Route::get('/games/edit/{game}', 'GamesController#edit');
GamesController.php
class GamesController extends BaseController {
...
public function edit(Game $game){
return View::make('/games/edit', compact('game'));
}
}
Pretty straight, but I get this error: Argument 1 passed to GamesController::edit() must be an instance of Game, instance of Illuminate\Http\RedirectResponse given
If I type http://mysite.dev/games/edit/1 all is fine (model with ID = 1 exists)
If I type http://mysite.dev/games/edit/12345 (no model with that ID) the ugly error above is triggered instead of the redirect I specified
I also looked at this (the bottom part where a Redirect closure is suggested: that is just what I am doing!) but no way to make it work: laravel 4 handle not found in Route::model
What's wrong with it? Please any help?
Thanks in advance
In Route::model you declare which variable will be a model instance, you shouldn't use it to do a redirection that way. Instead of that, specify that $game is of type Game and then work with your routes:
Route::model('game', 'Game');
...
Route::get('/games/edit/{game}', 'GamesController#edit');
Then if you access to /games/edit/3 GamesController::edit will receive an instance of Game class whose id=3
I ended up by setting a general "Not Found" error catcher, like this:
// routes.php
App::error(function(Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e) {
return Response::make('Not Found', 404);
});
...
Route::model('game', 'Game');
...
Route::get('/games/edit/{game}', 'GamesController#edit');
What I understand is that if I want a custom redirect and not a general 404 page (i.e. take the user to games' list if model not found), I CAN'T use the route-model-binding
In other words, I have to use Route::get('/games/edit/{id}', 'GamesController#edit'); and then do my application logic inside the 'edit' method:
public function edit($id){
$game = Game::findOrFail($id);
// if fails then redirect to custom page, else go on saving
}
I'm very new to Laravel, but as far as I can see this has nothing to do with the closure, but with the use of "Redirect::to" inside that closure. Using "App::abort( 404 );" works.

Resources