So sorry to bother y'all, but I was wondering if anyone could help me. I'm having a wee bit of a problem with my code in Laravel, being used to working from scratch rather than with frameworks. I made use of the Laravel Bootstrap Starter Site and I'm trying to add additional pages, but the routing isn't exactly co-operating. It's rather frustrating.
The Controller: app/controller/community/CommunityController.php
<?php
class CommunityController extends BaseController {
public function index() {
return View::make('community.index');
}
}
?>
The View
#extends('site.layouts.default')
{{-- Content --}}
#section('content')
#foreach ($posts as $post)
<div>
I'm just going to put this here...
</div>
#endforeach
{{ $posts->links() }}
#stop
And finally, last but not least, my routes.
Route::get('community', array(
'uses' => 'CommunityController#index',
'as' => 'community.index'
));
Now, I have this nagging feeling that I'm missing something rather small, but for the life of me I can't figure it out. If anyone would be so kind to explain what I'm doing wrong, I'd appreciate it. Especially since I can prevent this kind of problem happening in the future as well.
With friendly regards,
User who still hasn't picked out a good name
Edit: Sorry I forgot to mention this. I removed public, so I don't know if that influences anything. If it does, again, sorry for forgetting to mention this in the beginning.
you can try to bind the whole route to the controller, using
Route::controller('community', 'CommunityController');
then in your controller you have to prefix the controller methods with HTTP verbs.
Your index() method will be
public function getIndex() {
return View::make('community.index');
}
Just fire composer dump-autoload in root of your project folder from terminal / console.
It'll load your controller which is in subfolder.
Related
I have a weird situation, where my route seems to be not defined.
I am using Laravel's starter kit Breeze, and I am adding top left navigational links. This is my 4th link already, but it seems that It's the first time I face such issue.
Controller:
public function index(Request $request)
{
$user = $request->user();
$data = $user->campaigns;
return view('subscribes.index', ['data' => $data]);
}
Route:
Route::resource('/my_campaigns', App\Http\Controllers\SubscribeController::class);
Navigational link code:
<div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
<x-nav-link :href="route('subscribes.index')" :active="request()->routeIs('subscribes.index')">
{{ __('My Campaigns') }}
</x-nav-link>
</div>
Error message:
Route [subscribes.index] not defined.
I have spent 2 hours trying to check every single spot, where the issue may lay, but to no avail. I have already cleared route:cache.
I'd like to note, that the page /my_campaigns opens without any issues, and is working, until I try to add it within the navigational links.
My folders and files seems to be created correct as well, due to the fact, that /my_campaigns is working.
Any idea, where I missed something?
The issue was within my navigational link code. Where the href route is defined, I have used incorrect URL, therefore, this issue was caused.
it appears that when I created a new route, I receive the 404 error when trying to access the url, which is funny,. because all of my other routes are working just fine.
My web.php looks like so:
Auth::routes();
Route::post('follow/{user}', 'FollowsController#store');
Route::get('/acasa', 'HomeController#index')->name('acasa');
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Route::get('/profil/{user}/edit', 'ProfilesController#edit')->name('editareprofil');
Route::patch('/profil/{user}', 'ProfilesController#update')->name('updateprofil');
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Route::get('/alerte/url/{user}', 'UrlsController#index')->name('editurl');
Route::post('/alerte/url/{user}', 'UrlsController#store')->name('updateurl');
Route::get('/alerte/url/{del_id}/delete','UrlsController#destroy')->name('deleteurl');
The one that is NOT working when I am visiting http://127.0.0.1:8000/alerte is:
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The controller looks like so:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class PaginaAlerte extends Controller
{
public function __construct() {
$this->middleware('auth');
}
public function index(User $user)
{
return view('alerte');
}
}
I am banging my head around as I cannot see which is the problem. It is not a live website yet, I am just developing on my Windows 10 pc using WAMP.
Moved my comment to a little bit explained answer.
So, in your route collection, you have two conflicting routes
Route::get('/{user}', 'ProfilesController#index')->name('profil');
and
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Imagine that Laravel is reading all routings from top to bottom and it stops to reading next one after the first match.
In your case, Laravel is thinking that alerte is a username and going to the ProfilesController#index controller. Then it tries to find a user with alerte username and returning 404 because for now, you don't have a user with this username.
So to fix 404 error and handle /alerte route, you just need to move the corresponding route before /{username} one.
But here is the dilemma that you got now. What if you will have a user with alerte username? In this case, the user can't see his profile page because now alerte is handling by another route.
And I'm suggesting to use a bit more friendly URL structure for your project. Like /user/{username} to handle some actions with users and still use /alerte to handle alert routes.
The following route catches the url /alerte as well
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Since this one is specified before
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The /alerte will go the the ProfilesController instead.
To fix this change the order of the url definitions or change either of the urls to have nesting e.g. /alerte/home or /user/{user}
Well.
Maybe this is too late, but I have all week dealing with this problem.
I made my own custom.php file and add it in the routes path of my Laravel project, and none of the routes were working at all.
This is how I solved it:
You must remember to edit the RouteServiceProvider.php file located in app\Providers path. In the map() function, you must add your .php file. That should work fine!
To avoid unexpected behaviors, map your custom routes first. Some Laravel based systems can "stop" processing routes if no one of the expected routes rules were satisfied. I face that problem, and was driving me crazy!
I would wish suggest to you declare your URL without the "/", like your first "post" route, because sometimes, I have been got this kind of errors (404).
So, my first recomendation is change the declaration of the route. After that, you should test your middleware, try without the construct, and try again.
Good luck!
Updated after some research
After some research I conclude that, my sessions are not maintained until I save them explicitly, below code works well, but WHY???? Ref here
Session::put('lets_test', 2);
Session::save();
Old Question
I'm new to laravel 5.3, and stuck at a problem. My Laravel Session or Flash messages are not expiring they display each time page is reloaded, until I use Session::flush() Below is my controller code
<?php
namespace App\Http\Controllers;
use Session;
use Auth;
use Illuminate\Http\Request;
use App\User;
use App\Hospital;
use App\Wpr;
use Helper;
class OperatorController extends Controller
{
public $user_detail;
public function __construct()
{
$this->middleware('auth');
$this->middleware('operator');
}
public function store (Request $request){ //Form is being submitted here
//My logic here
Session::flash('user_message', 'Thank You');
return redirect('/operator/wpr');
}
}
I've also used Session::set('user_message', 4);
and blade view
#if(Session::has('user_message'))
<div class="message animated tada">
{{ Session::get('user_message') }}
</div>
#endif
I've tried with Session::forget('user_message') but no luck.
Updating my post after some research. I've got somewhat close to my problem by reading this post on stack because this question is exactly same to my problem but unfortunately it still persists, I've changed my session storage from file to database (in case file permissions to storage directory). What might be other possibilities?
Please help, thanks in advance.
Ultimately, somehow, I managed to find solution for my problem, of not sustaining sessions. I don't think its any issue related to file permission. Now I'm saving my session explicitly and removing it explicitly.
Made a Helper class added two methods
public static function SetMessage($message, $type){
Session::put('user_message', $message);
Session::put('user_message_type', $type);
Session::save();
}
public static function ForgetMessage(){
Session::forget('user_message');
Session::forget('user_message_type');
Session::save();
}
and within Controller class
Helper::SetMessage('Record updated successfully', 'success');
and within blade view tempalte
#if(Session::has('user_message'))
<div class="alert alert-{{ Session::get('user_message_type') }}">
{{ Session::get('user_message') }}
{{ Helper::ForgetMessage('user_message') }}
</div>
#endif
I hope this might help someone who is facing such kind problem. But why is it so, still unknown, maybe one day I'll post the reason too. More suggestions are welcomed, if this could be done in a better way.
try this:
if(isset($_SESSION['user_message'])) {
$message = $_SESSION['user_message'];
unset($_SESSION['user_message']);
return $message;
}
In your (Laravel) blade add $request->session()->forget('key'); or Session::forget('key'); at the end of if loop this will end or delete the session of that key. This may help you for more reference about session of laravel 5.3 visit laravel 5.3
I have been struggling with something very simple here nevertheless the vagueness around Laravel's routing system that complicates its easy routing approach. I have gone through questions listed here but nothing seems to help me so here it goes.
I have formerly defined a route to my controller on an action named "create". This action is suppose to accept a post data from the form and persist it. This create method has one parameter which defaults to null for a project id if its add else we pass an id e.g domain/projects/add/22 to edit and domain/projects/add to create a new one.
Below is the skeleton of the function:
public function create( $id = null ){ ... }
I then defined a route for this which is:
Route::post( 'projects/add', 'ProjectsController#create' );
Inside my form I have {{ Form::open(array('url' => 'projects/add', 'method' => 'post')) }} .
I keep on getting errors related to routing, Http or method not found exceptions. I tried to follow every suggestion on the net but cannot for the life of me find my way.
Please help me point to the right direction, thanks.
try with below sample code
routs.php►
Route::post('projects/add/{id?}',array('as'=>'project_create','uses'=>'ProjectsController#create'));
ProjectsController.php►
class ProjectsController extends BaseController{
public function create( $id = null ){
...
}
projects.blade.php► (for used blade templating your views should have blade.php extention )
<html>....
<form action="{{ route('project_create') }}"method="post">
....
</form>
</html>
Thank you guys for all your responses. After being swamped with work I finally came back to my project and wanted to try out some of your suggestions but I failed.
I did find a tutorial that I tried to follow and basically was able to have my routes working.
Inside the routes I added (see below):
Route::get('/projects/list', [
'as' => 'post.list',
'uses' => 'ProjectsController#listProjects'
]);
Inside my controller I just created a function that is named listProjects(). As for getting my form displayed I followed the same pattern except point to newProject() method in my controller.
As much as I was not keen on this approach I ended up creating another function just for saving my POSTed form data after a new project form has been filled out and submitted. I still used the same url as projects/add except pointing it to a different function in may controller named saveProject().
About the view I just added the as part of the same save route and it worked. below is a link to the tutorial I followed and taking a look at the code.
http://www.codeheaps.com/php-programming/creating-blog-using-laravel-4-part-1/
I am new to Laravel and was following a tutorial (https://www.youtube.com/watch?v=Zz_R73eW3OU&list=PL09BB956FCFB5C5FD). I have just installed the latest version of Laravel and am going through the beginning stages of the tutorial. So far (installation, creating a migration) I was doing ok, but now I am stuck at the point of creating new routes.
I have created a controller in app/controllers:
<?php
class Slots_Controller extends Base_Controller{
public $restful = true;
public function get_index(){
return View::make( 'slots.index' );
}
}
As well as a very minimalist view in the file app/views/slots/index.php
<h1>Slots</h1>
My app/routes file looks like this :
Route::get('/', function()
{
return View::make('hello');
});
Route::get( '/public/slots', array(
'uses' => 'slots#index'
) );
I've searched a number of things on the web and here is how far I have come :
- i know ideally only the public dir should be in the htdocs, i will be doing that presumably at a further stage of the tutorial. so as of now, my laravel home page is at http://localhost:8888/my_directory.laravel/public/
- i have checked the apache mod-rewrite : it is on and it works well for codeIgniter
- i have checked that the .htaccess is as provided by laravel (and as suggested by different posts)
The weird thing is, if i change the routes for the home page like this :
Route::get('/', function()
{
return 'hello world';
// return View::make('hello');
});
Nothing changes, i.e. i still get the original welcome page. My understanding is this modification should have simply output "hello world" as the html for the home page.
So it seems that whatever I'm doing in routes.php has no effect whatsoever, as if I was modifying the wrong file or something.
I've spent the most part of an otherwise sunny afternoon on this, any help is much apreciated :)
Thanks,
Tepp
There are many things you are doing wrong here (The best way is to follow the tutorial on Laravel):
In your controller file you are mentioning "Base_Controller", are you sure you have such a file in your controllers directory? (According to my knowledge it should be BaseController)
The Route::get is not defined properly. There are many ways to define a route; however for your case it should be something like this:
Route::get('slots', array(
'uses' => 'Slots_Controller#get_index'
));
Hope this helps.