Laravel web.php is not redirecting properly. - php

Below is my web.php content
Route::get('/myRoutes', function () {return view('myRoutes.routeIndex');})->middleware('auth');
Route::get('/myRoutes/{route}', 'routes#show')->middleware('auth');
Route::get('/myRoutes/create', 'routes#create');
Below is my resource controller(route.php)
<?php
namespace App\Http\Controllers;
use App\route;
use Illuminate\Http\Request;
class routes extends Controller
{
public function create()
{
return view('myRoutes.routeCreate');
}
public function show(route $route)
{
return view('myRoutes.routeShow', compact('route'));
}
?>
Now if i visit mydomain.com/myRoutes, it gives me a list of routes available.(working as expected)
If I visit mydomain.com/myRoutes/1, it gives me the record with id=1(working as expected)
Now the issue is if I visit mydomain.com/myRoutes/create, it returns a 404 error that the page not found. If I comment out the following line in web.php
Route::get('/myRoutes/{route}', 'routes#show')->middleware('auth');
then mydomain.com/myRoutes/create works as expected. I think laravel is confused between /myRoutes/{id} and /myRoutes/create. How to fix this?

in your web.php ,add create route before your show route.
Route::get('/myRoutes', function () {return view('myRoutes.routeIndex');})->middleware('auth');
Route::get('/myRoutes/create', 'routes#create');
Route::get('/myRoutes/{route}', 'routes#show')->middleware('auth');

Try putting your create route before your show route in web.php

Related

Laravel JSON file error message "target class does not exist"

I'm trying to load this JSON file from URL into my laravel project. It's showing this error from the host/data page. "Target class [App\Http\Controllers\SiteController] does not exist."
My routes web.php:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Controller;
use App\Http\Controllers\SiteController;
Route::get('/', function () {
return view('welcome');
});
Route::get('/data', [SiteController::class, 'index']);
And my controller which is in app/http/Controllers/Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SiteController extends Controller
{
public function index() {
$results = file_get_contents("http://ftp.ebi.ac.uk/pub/databases/genenames/hgnc/json/locus_groups/protein-coding_gene.json");
$data = json_decode($results, true);
dd($data);
}
}
Thank you!!
Better move your SiteController to App\Http\Controllers directory and then everything will be fine.
you may need read about namespaces in php.
i hope this help you.

Laravel controller not existing

I wanna redirect to my controller instead of a view but it says: "Target class [app/Http/Controllers/MyFirstController] does not exist. "
here is the code (web.php file):
//just a view
Route::get('/', function () {
return view('index');
});
//just a view
Route::get('/final', function () {
return view('welcome');
});
//the controller is interested in
Route::get('/hello-controller', 'app/Http/Controllers/MyFirstController#index');
Controller code (app/Http/controllers/MyFirstController.php) :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MyFirstController extends Controller
{
public function index(){
return "viva";
}
}
additional information:
Laravel Framework version: 8.83.17
PHP version : PHP 7.4.29
The namespace is not correct: Capital A for App and use \ instead of /
Route::get('/hello-controller', 'App\Http\Controllers\MyFirstController#index');
or even better :
Route::get('/hello-controller', [\App\Http\Controllers\MyFirstController::class, 'index']);
try to clear cache your controller by using
php artisan route:cache
and also maybe u should use
//the controller is interested in
Route::get('/hello-controller', 'App/Http/Controllers/MyFirstController#index');

Route [designations.addmore] not defined

I get an issue with the route as follows.
Route [designations.addmore] not defined
I tried to define routes but still i get the same error.Here is my code
View
Add more
Routes.php
Route::get('designations/addmore', ['as' => 'designations.addmore', 'uses' => 'Designations#addmore']);
Controller
<?php
namespace TCG\Voyager\Http\Controllers;
use Illuminate\Http\Request;
use TCG\Voyager\Facades\Voyager;
class Designations extends Controller
{
public function addmore()
{
echo 'hello';
}
}
Please help me.
Change your route like this
Route::get('designations/addmore', 'Designations#addmore')->name('designations.addmore');
and get route in anchor like this
view
remove second param from route because no param in your route.

Object not found error - When creating new pages in Laravel

I am new in Laravel. I am trying to create a new page named as "contact". But i am getting a Object not found error when i am trying to access the contact page
URL: project-name/contact
please help me
---routes file
<?php
Route::get('/','WelcomeController#index');
Route::get('contact','WelcomeController#contact');
Route::group(['middleware' => ['web']], function () {
//
});
--- Welcome controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class WelcomeController extends Controller
{
public function index(){
return view('welcome');
}
public function contact(){
return 'Contact page goes here...';
}
}
Set your home directory to the public to make things work.
Exchange the route. like this:
Route::get('contact','WelcomeController#contact');
Route::get('/','WelcomeController#index');

How the laravel process it's routes?

I am studying Laravel 5 as my new framework and I am following the video in Laracast and I got some weird error. I am displaying simple view in my controller but all I got is this error:
ModelNotFoundException in Builder.php line 125: No query results for model [App\Article].
Here's a bit of my code:
Route:
Route::get('/', 'WelcomeController#index');
Route::get('home', 'HomeController#index');
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
Route::get('articles', 'ArticlesController#index');
Route::get('articles/{id}', 'ArticlesController#show');
Route::get('articles/create', 'ArticlesController#create'); //returns error page
ArticlesController.php
<?php namespace App\Http\Controllers;
use App\Article;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ArticlesController extends Controller {
public function index() {
$articles = Article::all();
return view('articles.index', compact('articles'));
}
public function show($id) {
$article = Article::findOrFail($id);
return view('articles.show', compact('article'));
}
public function create() {
return 'Hello World'; //display error messages
}
}
So I just confused because when I try to access the create() method the Laravel also read the show() method.
Is this correct? So in the route list the Laravel will read it's routes from top to bottom?
So in my route in order to prevent the error I should put first the route of create before the show?
So it should be like this?
Route::get('articles', 'ArticlesController#index');
Route::get('articles/create', 'ArticlesController#create'); //make it first?
Route::get('articles/{id}', 'ArticlesController#show');
ModelNotFoundException is a db exception fired from the findOrFail method if the model your trying to find don't exist
check this redirect-if-model-doesnt-exists-modelnotfoundexception-doesnt-work-for-me
Because the /create also triggers the show url /{any}
You should do what you said, put the create route over the show route, or delegate that boring job using
Route::resource ('article', 'ArticleController')
Therefore you are getting a modelNotFoundException, because you are looking for an article with id = 'create'

Categories