Detecting 404 error page Laravel 4.2 - laravel

I'm trying to add a class to body if the page is an error page -- specifically 404.
Similar to
{{ Request::is('my-page') ? 'newclass' : '' }}
Is there any way to detect a specific error page?
My route:
App::missing(function($exception) {
return Response::view('error-404', array(), 404);
});

Pass the view name to each (or a specific) view as a variable. For Laravel 4 you can do this in the filters.php:
View::composer('*', function($view){
View::share('name_of_view', $view->getName());
});
Then you can do {{ ($name_of_view == "error-404") ? 'newclass' : '' }} in your views.

Related

laravel : passed data not be showed

i am new in laravel i made CRUD for some services. when i try to edit any one i have this error
Missing required parameter for [Route: services.update] [URI: dashboard/services/{service}] [Missing parameter: service].
my code to take data to edit it
<a href="{{route('services.edit', $service->id)}}">
my code in editing page {{ $service->id }}
and when i write in form to update in the editing page action="{{ route('services.update', $service->id) }}"
i got the error
and the code of edit in controller
public function edit(Service $service)
{
return view('dashboard.EditService', compact('service'));
}
in my rout page >>
Route::group(['prefix'=>'/dashboard' ], function () {
Route::get('/', function () {
return view('dashboard/index');
});
Route::resource('/services', 'App\Http\Controllers\ServiceController');
});
'Service' and 'Services' (with an 's' at the end) are different things according to laravel.
Try change your web.php file route parameter.
Before
Route::get('dashboard/services/{service}', [Controller::class, 'function']);
After
Route::get('dashboard/services/{services}', [Controller::class, 'function']);

Laravel - Method link_to_action does not exist

I am using Laravel 5.8.32. I used in my blade view:
{{ HTML::link_to_action('HomeController#index') }}
But it shows : Method link_to_action does not exist.
So why does it produce the error ? How to solve that ?
The HTML helpers has been deprecated in Laravel 5.0.
In Laravel 5.8, you can generate a URL to a given controller action with:
$url = action([HomeController::class, 'index'], ['param' => 'value']);
Since action is a global helper, you can use it in a view easily:
{{ action([HomeController::class, 'index']) }}

Laravel 5.8 handling all erros in one view

I am creating a all-in-one page for all errors in Laravl app. The error page should include a layout for currently logged in user: normal user, admin and guest. To be able to use Auth::check(), I created error page using fallback route.
Route::fallback(function () {
return view('errors.general', ['msg'=>'Error Description']);
});
And the view:
#php
if (Auth::guard('admin')->check())
$layout = "layouts.admin";
elseif (Auth::check())
$layout = "layouts.app";
else
$layout = "layouts.start";
#endphp
#extends($layout)
#section('content')
<div class="error-container">
<div class="error-box">
<div class="error-text">
{{ $msg }}
</div>
</div>
</div>
#endsection
The structure is working for 404 errors. But as for 500 errors, Laravel is showing default 500 page. How to direct all errors to use the same view with additonal error messages.
Instead of creating fallback route, handle all exception inside a Error Handler : app/Exceptions/Handler.php
And you can also check which user is logged in inside handler with below code it is just a basic example with which you can handle 500 errors like below code:
public function render($request, Exception $exception)
{
// 404 page when a model is not found
if ($exception instanceof ModelNotFoundException) {
if (Auth::guard('admin')->check()) {
// Your custom view for admin
} else {
//Your custom view for another user
}
}
if ($exception instanceof \ErrorException) {
if (Auth::guard('admin')->check()) {
// Your custom view for admin
} else {
//Your custom view for another user
}
}
return parent::render($request, $exception);
}
The fallback route is only catches non-existent routes (i.e., 404):
https://laravel.com/docs/6.x/routing#fallback-routes
But you can define your own error pages.
https://laravel.com/docs/6.x/errors#custom-http-error-pages
Laravel makes it easy to display custom error pages for various HTTP status codes. For example, if you wish to customize the error page for 404 HTTP status codes, create a resources/views/errors/404.blade.php.
In order to use your single error layout, you could do something like this:
resources/views/errors/500.blade.php
#include('layouts.error',['msg' => $exception->getMessage()])
EDIT: However, I do not believe that error pages load the session middleware, so you will likely have trouble attempting to use the Auth facade without additional effort.

custom validation, method not allowed on error

<form method="post" action="{{action('PLAYERController#update', $ply->reg_no)}}">
{{csrf_field()}}
Getting method not allowed exception on custom validation with false return. Tried mentioning PUT, PATCH and DELETE inside csrf field. Still, does not work.
UPDATE
using post for form method. Using method_field('POST'). not defining get method for the update function. If I go back from the error page back to the form page and press refresh, then the validation message is displayed as it should.
UPDATE 2
Validator::extend('check_sold_amount', function($attribute, $value, $parameters, $validator) {
if(($value%5000)==0)
{
return true;
}
return false;
});
}
UPDATE 3
Controller code
public function update(Request $request, $reg_no)
{
//
$this->validate($request, [
'sold_amount' => 'required|check_sold_amount',
'sold_to_team'=>'required'
]);
$ply = Player::find($reg_no);
$ply->sold_amount = $request->get('sold_amount');
$ply->sold_to_team = $request->get('sold_to_team');
$team_name=$ply->sold_to_team;
$team=Team::find($team_name);
if($ply->category=='indian')
{
$team->indian_players=$team->indian_players+1;
}
else {
$team->foreign_players=$team->foreign_players+1;
}
$team->balance=$team->balance-$ply->sold_amount;
$ply->save();
$team->save();
//return view('player.index');
}
Method not allowed means you do not have a route set up for the request that happened. There are 2 possibilities:
The route for the form submission is not set up
The code you have shown us does not include any method_field(...), so it looks like you are doing a normal POST. So you need a corresponding POST route like:
Route::post('/some/path', 'PLAYERController#update');
The route for the failed validation response is not set up
I'm guessing this is the problem. Say you are on pageX, and you landed here via a POST. You have a post route set up, so that you can land on this page OK. Now from this page, you submit the form you have shown us, but validation fails. When that happens, Laravel does a GET redirect back to pageX. But you have no GET route set up for pageX, so you'll get a Method not allowed.
Along with your POST route for the current page, you need a GET route to handle failed validations.
Also, you say Tried mentioning PUT, PATCH and DELETE inside csrf field - as others pointed out, you should use method_field() to spoof form methods, eg:
<form ...>
{{ csrf_field() }}
{{ method_field('PATCH') }}
Laravel form method spoofing docs
UPDATE
Based on your comments, I think it is actually your initial POST that is failing. Checking your code again, I think your action() syntax is incorrect.
According to the docs, if the method specified in the action() helper takes parameters, they must be specified as an array:
action="{{ action('PLAYERController#update', ['reg_no' => $ply->reg_no]) }}"
If your update route using patch method (example Route::patch('.....');) then add this {{ method_field('PATCH') }} below {{csrf_field()}} in your update form
UPDATE
If you are not using PATCH method for the update route, try this :
Route::patch('player/update/{reg_no}', 'PLAYERController#update')->name('plyupdate');
and then in the form you can do like below :
<form method="POST" action="{{route('plyupdate', $ply->reg_no)}}">
{{csrf_field()}}
{{ method_field('PATCH') }}
//Necessary input fields and the submit button here
</form>
This way always works fine for me. If it still didn't work, maybe there's something wrong in your update method in the controller
UPDATE
Untested, in your update method try this :
public function update(Request $request, $reg_no) {
$input = $request->all();
$ply = Player::find($reg_no);
$validate = Validator::make($input, [
'sold_amount' => 'required|check_sold_amount',
'sold_to_team'=>'required'
]);
if ($validate->fails()) {
return back()->withErrors($validate)->withInput();
}
$ply->sold_amount = $request->get('sold_amount');
$ply->sold_to_team = $request->get('sold_to_team');
$team_name=$ply->sold_to_team;
$team=Team::find($team_name);
if($ply->category=='indian') {
$team->indian_players=$team->indian_players+1;
}
else {
$team->foreign_players=$team->foreign_players+1;
}
$team->balance=$team->balance-$ply->sold_amount;
$ply->save();
$team->save();
return view('player.index');
}
Don't to forget to add use Validator; namespace
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
/**
* Show the profile for the given user.
........................
Did you reference a validation request controller?
Hope this helps :)

Posting form in Laravel 4.1 and blade template

I'm having trouble posting forms using Laravel 4.1 with the blade template engine. The problem seems to be that the full URL including http:// is being included in the form action attribute. If I hard code the form open html manually and use a relative url, it works OK, however, when it has the full url, I am getting an exception.
routes.php
Route::any("/", 'HomeController#showWelcome');
HomeController.php
public function showWelcome()
{
echo($_SERVER['REQUEST_METHOD']);
return View::make('form');
}
Form opening tag in form.blade.php
{{ Form::open(["url" => "/","method" => "post","autocomplete" => "off"]) }}
{{ Form::label("username", "Username") }}
{{ Form::text("username", Input::old("username"), ["placeholder" => "john.smith"]) }}
{{ Form::label("password", "Password") }}
{{ Form::password("password", ["placeholder" => ""]) }}
{{ Form::submit("login") }}
{{ Form::close() }}
So if I go to my home dir / in the browser, I see the form that I have created. If I fill in the form details and click submit, I am simply taken to the same page - the request method is still GET as shown by echo($_SERVER['REQUEST_METHOD']);
I notice that the full
http://localhost/subdir/public/
url is used in the form markup. If I hardcode a form open tag in such as
<form action="/subdir/public/" method="post">
it works fine and $_SERVER['REQUEST_METHOD'] shows as post.
What am I doing wrong here?
You have created the route for the post?
example:
{{Form::open(["url"=>"/", "autocomplete"=>"off"])}} //No need to later add POST method
in Route.php
Route::post('/', 'YouController#postLogin');
you have not set up a route to handle the POST. You can do that in a couple of ways.
As pointed out above:
Route::post('/', 'HomeController#processLogin');
note that if you stick with your existing Route::any that the `Route::post needs to be before it as Laravel processes them in order (I believe).
You could also handle it in the Controller method showWelcome using:
if (Input::server("REQUEST_METHOD") == "POST") {
... stuff
}
I prefer the seperate routes method. I tend to avoid Route::any and in my login pages use a Route::get and a Route::post to handle the showing and processing of the form respectively.

Resources