laravel route undefined ErrorException in UrlGenerator.php - php

I can't find solution to my Laravel routing error. Whenever I visit the link it shows the error 404 - page not found. Can any body help on this error. Below is my codes-
In web.php
Route::get('/post-ads-new/{cat}/{sub_cat}','PostController#postAdsNew');
In PostController.php
public function postAdsNew($cat,$sub_cat)
{
return view('post-ads-new');
}
And used the url in HTML as
<a href="<?php echo $url.'/post-ads-new/?cat='.$category->id;?>&<?php echo 'sub_cat='.$sub_category->subid;?>">

You should use Laravel URL generator by adding a name to your route
Route::get('/post-ads-new/{cat}/{sub_cat}','PostController#postAdsNew')->name('ads');
And in your view:
Blade version:
Native PHP version:
Here is the documentation https://laravel.com/docs/7.x/urls

The variables are unnecessary in the url, you will need to construct your url like the following:

Related

Laravel Route not defined error in resource controller

I have route file defined as follows...
Route::get('pincodes/export', 'PincodeController#export'); Route::resource('pincodes','PincodeController');
I have a controller called 'PincodeController' where the default functions works without any issue. I wanted to add a new function called 'export' in this controller. I tried {{ route('pincodes.export') }} in my blade file to generate link to this page... and it throws the following error...
Facade\Ignition\Exceptions\ViewException Route [pincodes.export] not defined. (View: /Users/tst/Desktop/www/laravel/project/resources/views/pincode/index.blade.php)
but if i access the url directly in browser it works fine. why is that ?
You need to specifically name the route:
Route::get('pincodes/export', 'PincodeController#export')->name('pincodes.export');
If you are using Laravel 5.5+, change export route like this
Route::get('pincodes/export', 'PincodeController#export')->name('export');
use route function in your blade file like
route('export')

Custom 404 laravel

If we just create a 404.blade.php page in resources/views/error it will work fine, but Auth() won't work on 404 page, to solve that if we follow the solution available on stackoverflow the Laravel auth errors will stop working. I use the following solution to do the work.
Create custom view
resources/views/errors/404.blade.php
in route.php
Route::any('{catchall}', 'PageController#notfound')->where('catchall', '.*');
create PageController and add this function
public function notfound()
{
return view('errors.404');
}
For Laravel 5.6 and later, you can use fallback in your routes\web.php:
Route::fallback('MyController#show404');
It works as an "catch all"-route.
See docs here.
Write below code in your Exceptions/Handler.php
if($this->isHttpException($exception)){
if(view()->exists('errors.'.$exception->getStatusCode())){
$code = array('status'=>$exception->getStatusCode());
return response()->view('errors.404',compact('code'));
}
}
Now create a new file in your view i.e errors/404;
Now in code array you can pass dynamic values
Not sure if this will help. Laravel has a PHP artisan command to publish error pages. Laravel Custom HTTP Error Pages
After you run this artisan command use Route:fallback() method as #KFoobar suggested. If you use a closure function, no need to use a controller. Make sure to add the below route at the ends of your routes file.
//Fallback/Catchall Route
Route::fallback(function () {
return view('errors.layout');
});
Shameless plug for my BLOG

Showing header and footer in CodeIgniter custom error pages

In my CodeIgniter folder I have got custom error pages for each type of error in application/views/errors/html like error_404.php etc. So when I tried to update the layout and added <?php $this->load->view('header'); ?> on top then it started to show me errors like:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Exceptions::$load
Filename: html/error_404.php
So I want to know how can I make it to work? I also tried to extends the Exception class from CI_controller but still no luck. Please help me to add my custom header to my custom error page. Thanks.
PS: I want to clear that even if I use some how PHP's include() function but I also want to use base_url function for e.g.
<script src="<?php echo base_url('assets/js/iconmenu.js'); ?>" type="text/javascript"></script> <br>
One more thing I want to clear that I am using Codeigniter 3.0 which provides all error's templates but don't allow to use $this->load function.
I don't know the reason for the above error. Might be it's because of core libraries are not loaded in case error 404 is happened.
You can try another way. See application/route.php and there is an option to override the 404 url, $route['404_override'] = '';
Example: Create a method error_404 in Welcome controller and update the route variable like $route['404_override'] = 'welcome/error_404';, now you can use whole helpers / libraries etc.
See routes
Hope this will help!

Laravel 5.4 function index try using function show

I have route:
Route::resource('admin/question', 'QuestionsController');
and function index:
public function index() {
return "Hello";
}
But when I try used index Laravel returned me the error:
Method [show] does not exist.
I'm using the link:
http://localhost:8012/siwz/siwz/public/admin/question
The server is WampServer program.
I can only use index function when I change route file:
Route::get('admin/question/index', 'QuestionsController#index');
Route::resource('admin/question', 'QuestionsController');
In Laravel version 5.3 I did not have to do it, it was enough to use:
Route::resource('.../...', '...Controller');
Actually, the URL is going to the correct function. admin/question should go to index. admin/question/{question} is the route that goes to show.
Take a look here and check how Laravel create Resource routes:
https://laravel.com/docs/5.4/controllers#resource-controllers
Since that you didn't provide your full routes. I am guessing that the link you were accessing is going to the wrong controller. You should check the ordering of the routes. Maybe you are accessing something like this on your route,
Route::resource('admin','AdminController');
And the AdminController doesn't have a method show(). Thats why laravel return that error.
Here's you can do
Comment out the rest of routes except route that you are accessing.
Try to reorder the affected routes. Maybe Laravel is confused with your route.

go to a new link in Laravel 4

suppose I want to go a new link like www.facebook.com. How do I do this in laravel 4. If i use return Redirect::to() then I get the error that
there is no method called www.facebook.com
in my controller. If I use header('Location: ' . $url); then I get the error
Undefined variable: content
since there is a code in my controller which says protected $layout = 'sites.master' in in the layouts/master file there is a {{$content}}
You can use the URL::route('named_route')') function inside the href attribute in the blade syntax to go to a particular named route and URL::to('link_adress') function inside the href attribute in the blade syntax to go a particular custom address.
Example code for creating a link to google would be like:
<a class="link" href="{{ URL::to('http://www.google.com') }}">Go to google</a>
The view should use the blade templating engine for this to work.

Categories