How to use namespace in Laravel in controller? - laravel

I have a custom class in App/Helpers/regexValidation.php:
<?
namespace App\Helpers;
class RegexValidation
{
public static function isCVC($str)
{
$re = '/^[0-9]{3,4}$/s';
preg_match($re, $str, $matches);
return $matches;
}
}
I try to use it in controller:
<?php
namespace App\Http\Controllers;
use App\Helpers\RegexValidation;
public function detectfile(Request $request)
{
$cvc = RegexValidation::isCVC(555);
}
When I call this method from route Laravel I get:
Error: read ECONNRESET
If comment this line $cvc = RegexValidation::isCVC(555); it works.
What do I do wrong?

I think you are having a PSR4 Error,
I have a custom class in App/Helpers/regexValidation.php
Rename that file to PascalCase as RegexValidation.php
after renaming run
composer dumpautoload
And it should work.

Related

Laravel 5.7 SendEmail is not working properly

I am sending email using laravel 5.7 but i found following error " (1/1) FatalErrorException
Class 'App\Mail\UserRequest' not found" Need help
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
//use \App\Mail\SendMail;
use App\Mail\UserRequest;
class ContactusController extends Controller
{
public function index()
{
return view('contactus');
}
public function sendmail(Request $req)
{
$msgdata = array('subject'=>$req->subject,'email'=>$req->email,
'name'=>$req->name,'body'=>$req->message);
Mail::to('dddddddd#dddsdsf.com')->send(new UserRequest($msgdata));
return view('contactus');
}
}

Laravel | Class not Found

I would like ot integer an externe class with two functions into my laravel project. This externe class serves for e-payment.
I created a new folder in \App named xxx then my php file named xxx.php.
My path is
\App\xxx\xxx.php
When i would like to call this class in XController using this code :
<?php
namespace App\Http\Controllers;
use App\xxx\xxx;
class XController extends Controller
{
public function Send(Request $request){
$function = new xxx;
};
}
A got an Error : Class 'App\xxx\xxx' not found
my xxx.php code is like that :
<?php
namespace App;
class xxx
{
public function function($data)
{
return ;
}
}
My route :
Route::get('/send', 'XController#Send');
Thank you in advance !
Correct the namespace in your class file Xxx.php
<?php
namespace App\Xxx;
class Xxx
{
public function function($data)
{
return ;
}
}
and run this command in your console
composer dump-autoload
Laravel uses PSR-4 autoloading, so you should ensure that your classes and folders are capitalised. You can read up more on PSR-4 here - https://www.php-fig.org/psr/psr-4/
In your case the folder should be Xxx instead of xxx and your file should be called Xxx instead of xxx.
At the same time you also need to update the namespace on the Xxx.php file:
<?php
namespace App\Xxx;
class Xxx
{
public function function($data)
{
return ;
}
}
Then within your controller you can update your use statement and the method.
<?php
namespace App\Http\Controllers;
use App\Xxx\Xxx;
class XController extends Controller
{
public function Send(Request $request){
$function = new Xxx;
}
}
I hope this helps.
in xxx.php file namespace should be like this :
namespace App\folderName; // App\xxx;
and then run
php artisan config:cache;

After adding a new directory into controllers, fatal error thrown

I am new to Laravel and after I added a new directory to the controllers viz, Admin, I updated the namespace also updated routes but somehow, I am getting a fatal error exception. Please help me figure out the problem
App->Http->Controllers->Admin
<?php
namespace App\Http\Controllers\Admin;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
}
routes.php
Route::get('/admin', 'Admin\AdminController#index');
snapshot of directory structure
try
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function index()
{
echo "admin controller";
}
}
Note the "use" statement. I suspect that Laravel is looking for the Controller class, which this controller extends, but is unable to find it because of that missing statement.

Error on finding controller from Laravel API Router

I created a fresh Laravel framework.
I created a controller named PostsController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\Http\Controllers\Controller;
class PostsController extends Controller
{
public function index()
{
$posts = Post::get();
return response()->success(compact('posts'));
}
}
Then I created a route in the file api.php:
Route::get('posts', 'PostsController#index');
I ran the command
$ php artisan serve`
and I tested the URL
localhost:8000/api/posts
This error occurs:
BadMethodCallException
Method Illuminate\Routing\ResponseFactory::success does not exist.
file: vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php
line: 100
throw new BadMethodCallException("Method {$class}::{$method} does not exist.");
I can't understand why this happened. Please help me.
There is no success method on the ResponseFactory. You can find the available methods here.
You are calling a macro function that is not registerd to the responseFactory.To use success method,Create your custom responseServiceProvider and write this inside boot()
Response::macro('success',function($data){
return Response::json([
'data'=>$data,
]) ;
});
And then register your ResponseServiceProvider to app.php by adding your Class name to the array called providers. This is how you add to the array
App\Providers\ResponseMacroServiceProvider::class

Call to undefined method Illuminate\Notifications\Notification::send()

I am trying to make a notification system in my project.
These are the steps i have done:
1-php artisan notifications:table
2-php artisan migrate
3-php artisan make:notification AddPost
In my AddPost.php file i wrote this code:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class AddPost extends Notification
{
use Queueable;
protected $post;
public function __construct(Post $post)
{
$this->post=$post;
}
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
'data'=>'We have a new notification '.$this->post->title ."Added By" .auth()->user()->name
];
}
}
In my controller I am trying to save the data in a table and every thing was perfect.
This is my code in my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\User;
//use App\Notifications\Compose;
use Illuminate\Notifications\Notification;
use DB;
use Route;
class PostNot extends Controller
{
public function index(){
$posts =DB::table('_notification')->get();
$users =DB::table('users')->get();
return view('pages.chat',compact('posts','users'));
}
public function create(){
return view('pages.chat');
}
public function store(Request $request){
$post=new Post();
//dd($request->all());
$post->title=$request->title;
$post->description=$request->description;
$post->view=0;
if ($post->save())
{
$user=User::all();
Notification::send($user,new AddPost($post));
}
return redirect()->route('chat');
}
}
Everything was good until I changed this code:
$post->save();
to this :
if ($post->save())
{
$user=User::all();
Notification::send($user,new AddPost($post));
}
It started to show an error which is:
FatalThrowableError in PostNot.php line 41: Call to undefined method
Illuminate\Notifications\Notification::send()
How can i fix this one please??
Thanks.
Instead of:
use Illuminate\Notifications\Notification;
you should use
use Notification;
Now you are using Illuminate\Notifications\Notification and it doesn't have send method and Notification facade uses Illuminate\Notifications\ChannelManager which has send method.
Using this
use Illuminate\Support\Facades\Notification;
instead of this
use Illuminate\Notifications\Notification;
solved the problem for me.
Hope this helps someone.
using this is better
use Notification
Instead of
use Illuminate\Support\Facades\Notification
this makes the send() not accessible [#Notification Databse]

Resources