I was having a hard time with the Laravel auth package. If you use the out-of-the-box Laravel Auth, if you try to access a page you don't have access to the Auth will redirect you to the login page and then after a successful login redirect you back to the page you were trying to access.

This works fine. But if I go to a page and then click on login it would redirect me back to a page specified in the Auth controller instead of to the page I was on before I clicked login. I searched for a while and found some info, but not much addressing this specific issue. 

I finally found this thread on Laracasts which gives a simple and easy solution to the problem. 

The solution is to override the login form method in the LoginController.php in the app directory. I added this function:

public function showLoginForm(){
     if(!session()->has('url.intended')){
          session()->put('url.intended', url()->previous());
     }
     return view('auth.login');
}

This pushes the previous page onto the session as url.intended, which is the same thing the middleware does. But this does it in all cases, not just when the middleware catches an auth error. After login the Auth controllers now send you back to url.intended instead of to the default page specified in $redirectTo.

Libellés: coding, laravel
Aucun commentaire

The Archive List on the Right Here

mercredi 19 octobre 2016

It was a bit tricky for me to get the archive list on the right here working properly. It was easy to do in normal PHP, but I wanted to stay within a strict MVC model where all the processing is done in the controller and the view just displays the data. I was having a hard time figuring out how to properly group the items without setting variables in the view.

I ended up building an array with three nested levels in the controller. The array is as follows:

  • $year => [
    • $month =>[
      • 'title' => $title
      • 'slug' => $slug ]
    • ]

I start with my array with the $year as key and an empty array as the value. Then for each month I push on an array with $month as key and an empty array as the value. Then for each post I push on an array with two values - the post $title and $slug. This array is passed into the view.

To display it I just do three nested foreach loops:

  • foreach($array as $year => $months)
    • // output $year and the corresponding HTML for the collapse panels
    • foreach($months as $month => $posts)
      • // output $month and the HTML for the collapse panels
      • foreach($posts as $post)
        • // output $post['slug] and $post['title']

Simple, clean and easy! Much simpler than the other ways to do this I found online. 

Libellés: coding
Aucun commentaire

First Post

jeudi 16 juin 2016

I don't really have much to say, and I may not ever post here again. I'm just doing this to teach myself Laravel. But I can't just leave this page blank with nothing here, so I figured I should write something. 

Honestly, I don't much care for blogs. I don't really think my opinions are so great that everyone needs to read them and I certainly don't want to read everyone else's opinions about everything. I used to keep a blog on blogger where I would post random things I found, but I haven't updated it since 2009. 

But writing a little blog module seemed like a good way to learn so I made this. And it would be really silly if it was just a blank page with nothing here. 

Libellés: test
4 commentaires

Archives du Blogue