I'm trying to work with some ajax inside of laravel, still quite new to all of it, but I've noticed that for some reason everytime I create a new route I get a 404 regardless of what it is that I'm doing. For example, something as simple as:
Route::get('templateswitch', function() {
return 'Hello World';
});
Will still return out a 404, it is listed in the routes, and I've tried running the optimization through artisan too, so far no dice.
I'm not sure if it's just something I'm doing wrong in the code or something wrong with my server.
Any help would greatly be appreciated!
Full web.php
<?php
use App\Http\Controllers\SwitchController;
use App\Http\Controllers\TemplateSwitch_Controller;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('templateswitch', function() {
return 'Hello World';
});
Route::post('/switch', [TemplateSwitch_Controller::class, 'ajaxGetByID'])->name('test_templateswitch');
Route::get('/', [TemplateSwitch_Controller::class, 'index']);
Related
I'm trying to render a view but I always get notfoud 404 I don't know how to solve this question anymore if anyone can help me I would be grateful.
any help is welcome.
Thanks in advance.
Everyone have a great Monday.
CommunityPostController -> code ->
public function creategif(Community $community)
{
return view('posts.creategif', compact('community'));
}
Web Routes
Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('u/{id}', [App\Http\Controllers\HomeController::class, 'user'])->name('user.view');
Auth::routes(['verify' => true]);
Route::group(['middleware' =>['auth', 'verified']], function (){
Route::resource('communities', \App\Http\Controllers\CommunityController::class);
Route::resource('communities.posts', \App\Http\Controllers\CommunityPostController::class);
Route::resource('posts.comments', \App\Http\Controllers\PostCommentController::class);
Route::get('communities/{community}/posts/creategif', [\App\Http\Controllers\CommunityPostController::class, 'creategif']);
Route::get('posts/{post_id}/vote/{vote}', [\App\Http\Controllers\CommunityPostController::class, 'vote'])->name('post.vote');
});
Views Files Structure
There might be a conflict base on your route order and the image you've posted. As you can see you have resource for your route which in this case this conflict might happen base on route order, and your laravel is actually trying to get a post instead of loading creategif route.
Which in this case every time you try to get access to creategif route, your application is actually trying to load a post base on 'communities.posts' route.
So base on this conflict in your route order, this is working fine and create or any other routes related to 'communities.posts' should work fine, but in other-hand creategif in URL might recognized as a route related to 'communities.posts' resource.
Move your route to top, or in this case just above 'communities.posts' route, don't forget to clear route cache.
Route::group(['middleware' =>['auth', 'verified']], function (){
Route::get('communities/{community}/posts/creategif', [\App\Http\Controllers\CommunityPostController::class, 'creategif']);
Route::resource('communities', \App\Http\Controllers\CommunityController::class);
Route::resource('communities.posts', \App\Http\Controllers\CommunityPostController::class);
Route::resource('posts.comments', \App\Http\Controllers\PostCommentController::class);
Route::get('posts/{post_id}/vote/{vote}', [\App\Http\Controllers\CommunityPostController::class, 'vote'])->name('post.vote');
});
i bought a laravel script
it's working fine but any route i try to add i get error
here is simple route
Route::get('/greeting', function () {
return 'Hello World';
});
not working but dashboard is working anything in the script work fine but adding any route i get that error
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Auth::routes();
Route::group(['middleware' => 'auth'], function() {
Route::get('/dashboard', 'HomeController#dashboard');
});
Route::get('/greeting', function () {
return 'Hello World';
});
As mentioned in the comment by IGP, you should try to clear the route cache first.
If that does not help, you should take a look at the RouteServiceProvider to make sure that your route file gets called from there.
You can also try to adjust the order (if more than one routing file is called) so that your route file is evaluated before the others (the first matching route "wins").
Also please check the route config of the bought script as it could contain a catch-all/wildcard route.
IGP solution work fine it was a cache problem
thank you
I tried to get an answer from the laracast community but ended up not getting a response. I get the following text output at the top of every page or CLI command:
Skip to content Personal Open source Business Explore Pricing Blog Support This repository
3,019 22,483
7,384
laravel/laravel Code Pull requests 0 Pulse Graphs
laravel/config/database.php eb7743f 11 days ago #dakira dakira allow
for setting sqlite database via env #taylorotwell #TGM #vlakoff
#dakira #marcanuy #pedes42 #jimmypuckett #GrahamCampbell
#david-ridgeonnet #SyntaxC4 #overtrue 121 lines (101 sloc) 3.84 KB
All of the files that may be suspect are available in the laracast discussion I posted.
C4 #overtrue
121 lines (101 sloc) 3.84 KB
BTW I used composer to install laravel.
EDIT routes.php file
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', 'PagesController#index');
Route::get('item', 'ItemController#index');
Route::get('item/create', 'ItemController#create');
Route::get('item/{id}', 'ItemController#show');
Route::get('welcome', function() {
return view('welcome');
});
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController#index');
});
Route::post('item', 'ItemsController#Store');
Hello what I am trying to do is fairly simple I know it can be done with routes.php I am confused about how to do it, I would like all logged in users to not be able to see "/"
so when they try to access '/' they get send to '/name'
Route::get('/', function() {
// ...
return Redirect::to('/name');
});
this doesnt check if they are logged in
Use before filters:
Route::get('/', function() {
return View::make('your-home-view-comes-here');
})->before('auth');
Edit the filters.php file and change it to:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::to('/name');
});
you'd also might want to group the routes to a filter as explained in
laravel's route groups
this will be easier to manage if you want that filter to run for multiple route item.
I'm creating an authorization system in my Laravel 4 project. I am trying to use the auth "before" filter.
In my routes.php file, I have:
Route::get('viewer', array('before' => 'auth', function() {
return View::make('lead_viewer');
}));
Route::get('login', 'LoginController');
The before filter calls this line in the filters.php file:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::route('login');
});
I can manually navigate to my login route. But the auth system isn't letting this happen. I've run composer dump-autoload a couple of times, so that isn't the problem. What am I doing, since I can actually load the login page if I do it manually?
I figure it out. Laravel is looking for a named route: I had to do this:
Route::get('login', array('as' => 'login', function() {
return View::make('login');
}));
An interesting, not very intuitive approach in Laravel. But there must be a reason Taylor did this that I'm not seeing.
To do what you were trying to do in your initial approach you could have just done:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::to('/login');
});
and it would have worked just fine.
If you want to use named routes then you do what you posted in your answer to your own question. Essentially...more than one way to skin a cat.
Hope that helps
I know you've probably solved this by now but after stumbling across your post while trying to solve a similar problem, I wanted to share my thoughts...
Laravel is NOT looking for a named route for the guest method, it is expecting a path.
Your example works because because the named route and the path are the same i.e. "login". Try changing your URL to something other than 'login' and watch it fail.
If you want to use a named route you should use the route helper method as so...
if (Auth::guest()) return Redirect::guest( route('login') )
Hope that helps someone.