Non-static method Illuminate\Http\Request::url() cannot be called statically - php

I'm using Laravel 9 and I want to redirect user to same page url, so I tried this:
if(!$status){
alert()->error('Wrong code!');
return redirect(Request::url());
}
And I have included as well the Request but don't know why get this error:
Non-static method Illuminate\Http\Request::url() cannot be called statically
So what's going wrong here? How can I solve this issue?

You can use
return redirect()->back()->withInput();
Or
use Illuminate\Http\Request;
public function yourMethod(Request $request) # inject $request here
{
return redirect($request->url());
}

Related

Laravel 5.3 class/routes

Hej! My question is the next. I started to use Laravel 5.3. How can I do this class in Laravel:
class Vehicle
{
public $vehicletype;
function invt($vehicletype){
$this->vehicletype=$vehicletype;
}
function outvt(){
return $this->vehicletype;
}
}
I already have ajax .post ,the route:
Route::get('/ajax-vehicletype',function(){
$vehicletypevalue=Input::get('vehicletype');
Vehicle::invt($vehicletypevalue);
});
I get error:
Non-static method App\Vehicle::invt() should not be called statically, assuming $this from incompatible context
Thanks.
You can define controller and then define function inside that controller and pass that in your route argument.
class Vehicle extends Controller{
public $vehicletype;
function invt($vehicletype){
$this->vehicletype=$vehicletype;
}
function outvt(){
...........
}
}
In your route file you can define the route as
Route::get('/ajax-vehicletype/{vehicletype}','Vehicle#invt');
You need to learn basic PHP first.
The problem you are facing is because you are calling non-static method statically as it is said in the error. The quick fix would be to define invt method as static: http://php.net/manual/en/language.oop5.static.php

Call to a member function success() on a non-object

I have tried many times this error in different ways also tried one sollution from: CakePHP: Call to a member function setFlash() on a non-object url ... But this solution is also not working in my project.
Cant recognize whats the error. !!!
Here is my User.php model
class User extends AppModel {
public $helpers = array('Html','Form');
public $components = array('Email','Flash','Session');
public function send_mail($useremail){
$Email = new CakeEmail('gmail');
$Email->emailFormat('html')
->from('abc#xyz.com')
->to($useremail)
->subject('User subject');
if($Email->send("123")){
$this->Flash->success(__('Mail Sent')); // **this line cause error**
}else{
$this->Session->setFlash('Problem during sending email');
}
}
}
As the documentation suggest, FlashComponent provides two ways to set flash messages: its __call()magic method and its
set() method. To furnish your application with verbosity, FlashComponent’s __call() magic method allows you use a method name that maps to an element located under the src/Template/Element/Flash directory. By convention, camelcased methods will map to the lowercased and underscored element name:
// Uses src/Template/Element/Flash/success.ctp
$this->Flash->success('This was successful');
// Uses src/Template/Element/Flash/great_success.ctp
$this->Flash->greatSuccess('This was greatly successful');
Alternatively, to set a plain-text message without rendering an element, you can use the set() method:
$this->Flash->set('This is a message');
And hence, you need to change it by typing
$this->Flash->success('Mail sent');

Add a route to handle multiple HTTP requests in Slim PHP

I am using the following code for "Routing using methods inside classes:"
$app->any('/contacts', 'Contacts:home');
My class looks like:
class Contacts {
public function home() {
return 'something';
}
}
The above code works fine for me and when I open "http://localhost:3000/contacts"
The Problem is when I try to handle multuple HTTP request
$app->group('/users/{id:[0-9]+}', function() {
$this->map(['GET', 'POST'], '', 'Users');
});
Is there anyway, I can pass class name such as Users in the above pseudo code and the code works for me, The class would be something like:
class Users {
function get() {
return 'asd';
}
function post() {
return 'post';
}
}
In such a way, that my request listens to the appropriate method.
You would need to create a method that sorts out the current route's details than calls the correct method.
You can determine which method was used by calling the $request->getOriginalMethod(); function, then using call_user_func_array(); function you can call whichever of your functions is appropriate for the current method.

store and update in Laravel - which Request to use?

I'm new to Laravel and I have problems with storing and updating a Model.
Here is my store method
public function store(Request $request)
{
$input = Request::all();
Klijent::create($input);
return redirect('klijenti');
}
And I have to include use Request; to make it work.
Here is my update method
public function update(Request $request, $id)
{
//
$klijent = Klijent::find($id);
$klijent->update($request->all());
return redirect('klijenti/'. $id);
}
And I have to include use Illuminate\Http\Request; to make it work.
But if I don't use first one I get this error when using store method:
Non-static method Illuminate\Http\Request::all() should not be called statically, assuming $this from incompatible context
If I don't use second one, I get this error when I use update method:
Call to undefined method Illuminate\Support\Facades\Request::all()
If I use both of them I get this error:
Cannot use Illuminate\Http\Request as Request because the name is already in use
You need to make the call to non-static methods like
$input = $request->all();
in your first function. The second error is because Illuminate\Support\Facades\Request does not have an all method to call. The third error is a namespace conflict, because you cannot have two classes with the same name in PHP.

Laravel Resource Controller missing Method

I have a laravel resource controller as follow:
BlogController.php
class AdminBlogController extends BaseController {
public function index()
{
// some code
}
public function create()
{
// some code
}
// etc.....
in route.php I have this :
Route::resource('blog', 'AdminBlogController');
now I understand that when you go to URL /blog , it goes to index() and when you go to /blog/create goes to create() method.
My question is how do I handle missing method? for example when some types /blog/test , I get an error there , how can I redirect back missing methods to /blog?
Thanks
Taken from the Laravel Documentation:
If you are using resource controllers, you should define a __call
magic method on the controller to handle any missing methods.
In your AdminBlogController, add a __call magic method:
public function __call($method,$parameters = array())
{
if (!is_numeric($parameters[0])) {
return Redirect::to('blog');
}
else {
$this->myShow($parameters[0]);
}
}
...and, importantly, you need to rename your show method to something else (myShow in this example). Otherwise, /blog/test will route to show with the expectation that test is an id of a blog that you want to show. You also need to specify, in your __call method, which parameters after blog/ should be considered IDs, and which should be considered missing methods. In this example, I allow any numeric parameter to be treated as an ID, while non-numeric parameters will redirect to Index.

Categories