Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[solved] extending the core - total noob stuff here #5

Closed
gyoza opened this issue Feb 27, 2015 · 35 comments
Closed

[solved] extending the core - total noob stuff here #5

gyoza opened this issue Feb 27, 2015 · 35 comments

Comments

@gyoza
Copy link

gyoza commented Feb 27, 2015

Hi, I am kind of new to laravel, I do web development for fun, its not my job, I am a sysadmin so I am learning still.

Ok, now that that is out there.

I am having issues with I believe are Routes not passing data properly when using angel. I've extended the angel core so i can have my own custom pages.

I am trying to create a form that routes data to a new page. I get the page to route fine and checking the post data i see my normal data but i get an error when it attempts to display the next page.

I am getting

Undefined variable: page (View: /var/www/email/app/views/pages/returnmail.blade.php)

which is essentially a modified copy of the main blade file with some small modifications.

There is a "pages" view added in pages. Any help would be REALLY appreciated, I love this project so far and we all love

@JVMartin
Copy link
Owner

Hi gyoza,

When you extend the PageController, you'll notice that the PageController has this vital line (highlighted):
https://github.com/JVMartin/angel/blob/master/src/controllers/PageController.php#L15

$this->data['page'] = $page;

That is where $page is loaded into the $data array. The $data array is then passed to all views, because its elements are referenced everywhere. It also contains all messages (errors, success, etc.). For instance, when showing the title of the page in the front-end page blade you can see that $page is accessed:
https://github.com/JVMartin/angel/blob/master/src/views/page.blade.php#L3

The page that is loaded is from the URL specified from this route variable here:
https://github.com/JVMartin/angel/blob/master/src/routes.php#L208

Note that this route has a variable in its very base, letting us create pages for URLs like /about-us and /contact, etc. In order to make a new page with an URL, it has to be in the database if you're going to load it from the PageController in this fashion.

I'm available on Skype (JunkGrave) if you'd like to share your screen, I could walk you through anything you'd like to accomplish!

Hope this helps.

-Jacob

@JVMartin
Copy link
Owner

Also, have you seen this tutorial I made?
https://www.youtube.com/watch?v=6iuZ8p-x5bY

I don't think I've put the link in the docs yet, I'll have to do that.

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

So pretty much i have to inject the database with the code of the page?

as for the tutorial you kind of click and bop around in folders without the full path showing and you do this quickly so it is hard for me to keep up hehe.

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

Maybe I just need to change my submit button to use your route then?

@JVMartin
Copy link
Owner

You don't necessarily have to put any code in the database. In that video, I'm writing the home.blade.php with its own HTML code / scaffolding, and only the content is loaded from the DB. I usually just put content in the CMS database, while keeping all the actual HTML code for the structure of the pages in the blade files like this. If you look at the left hand side of the screen, you can see the full directory structure for my workflow layed out in the IDE that I have open. All I'm saying above is that if you want to use the $page variable (which is used by default from the page.blade.php we extend for many pages), it is a model loaded into the views from the PageController@show method. In that case, the page needs to have the same URI as the route being requested and as the method being called. (The video goes through this carefully.)

As for your form, you can have it post to any route and controller you'd like and process it however you'd like! Angel doesn't modify Laravel in any way, it's just a set of controllers and models to add some basic CMS functionality.

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

Well I have a page called page2 created in angel and my route takes my sanitized form data and send its through my route for my button and then pushes it to /page2 and i get the missing page error.

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

home.blade.php

{{ Form::open(array('method' => 'PUT', 'action' => 'PageController@returnmail')) }}

plus other junk and a submit button.
{{ Form::submit('Save') }}
{{ Form::reset('Reset') }}
{{ Form::close() }}

routes.php


Route::put('/returnmail', 'PageController@returnmail', array('title'=>'returnmail', 'page'=>'returnmail'));
Route::post('/returnmail', 'PageController@returnmail', array('title'=>'returnmail', 'page'=>'returnmail'));
Route::post('form-submit', array('title'=>'returnmail', 'page'=>'returnmail','before'=>'csrf',function(){

// form validation here.

}

get this error when i post the form.



ErrorException

Undefined variable: page (View: /var/www/project/app/views/pages/returnmail.blade.php)
open: /var/www/project/app/storage/views/626caa1645f58d41bbd44f02136330c6

    <?php $__env->startSection('title', $page->title); ?>

    <?php $__env->startSection('meta'); ?>
    <?php echo $page->meta_html(); ?>
    <?php $__env->stopSection(); ?>
    <?php $__env->startSection('css'); ?>
    <?php if ($page->css): ?>
    <style>


i think thats everything..

@JVMartin
Copy link
Owner

The PageController is for displaying pages. You should not be posting form data to it in my opinion. It's also not considered good practice to be doing validation from a Route:: function closure like that.

All you need to do to create a form in Laravel is...

View:

{{ Form::open(array('route'=>'contact_form')) }}
    {{ Form::hidden('test', 'test') }}
    {{ Form::submit('Send') }}
{{ Form::close() }}

Route:

Route::post('contact-form', array(
    'as' => 'contact_form',
    'before' => 'csrf'
    'uses' => 'ContactController@submission'
));

Controller:

class ContactController extends BaseController  {
    public function submission()
    {
        echo 'Received data: ' . Input::get('test');
    }
}

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

Well, i am not trying to make a contact page, I am trying to just post data from one page to pull it from the next page nothing fancy, nothing needs to be stored, its just like a choose your own adventure type thing, based on the values you pick on the first page will then display certain things on the next page.

Aside from doing things wrong in your opinion, based on what i showed you, I am passing the right page data correct? This should be working?

p.s. angelvision is like going out of business:)

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

I really like angel and it would be cool if we could work together to make it so noobs like myself could easily get a page going and learn some laravel at the same time :P

If it was a little bit user friendly to add custom code into the pages it would be a whole lot simpler. But I really do like it :)

And yes your tutorial did help a bunch, i obviously got it extended and working but it just took me having to watch it a number of times:)

@JVMartin
Copy link
Owner

I was merely using an example. You could do it with literally anything, not just contact forms.

I honestly just don't understand your code. For instance:

Route::post('/returnmail', 'PageController@returnmail', array('title'=>'returnmail', 'page'=>'returnmail'));

I have never, in all my years of experience, seen someone passing an array as a 3rd paramater to Route::post like that. I don't see that anywhere in the Laravel 4.1 documentation, either. I don't know what that is.

And Angel is getting simplified quite a bit for Laravel 5... but, at the end of the day, anything as complex as a CMS does require some proficiency with Laravel. Angel is the simplest CMS I've seen so far, but everything could always be simpler! :)

@JVMartin
Copy link
Owner

Long story short: In order to use the $page variable, you have to make sure it's being passed to the view correctly. If it's not, you'll get that error that it doesn't exist!

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

I was just fooling around trying to set that page variable trying anything I could muster thats all;)

@JVMartin
Copy link
Owner

Ah I see. Cool cool... well I hope that you got all sorted! You can always copy the code from show() if you need to set up the $page variable yourself. Just be sure to pass all of the $data array into your views!

(Because you're not using the show method like every other page does, you'll have to do anything that it does that is crucial to the views' variables such as loading the $page model.)

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

What file can I find that in?

@JVMartin
Copy link
Owner

This is the PageController, where the show() method lives that all pages use:
https://github.com/JVMartin/angel/blob/master/src/controllers/PageController.php#L15

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

Oh i guess i forgot that i extended the page controller as well..

    public function returnmail()
    {
            //echo "WE WANT THIS TO RETURN THE TEMPLATES BUT ALSO USE OUR DATAZ";
                        return View::make('pages.returnmail', $this->data);

    }

which got me to where i am.

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

I think I know what I did wrong now.. I never properly copied over the page controller code i guess lol.

@JVMartin
Copy link
Owner

You shouldn't copy over the PageController code... you've already extended the \Angel\Core\PageController with your local PageController so you're all set, right? I definitely cover all of this in the videos. You don't copy code from the core, you simply extend the classes and overwrite or add any methods you need.

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

So i guess I just need to jam in the show code into my returnmail() function?

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

As it stands with the function returnmail() and me doing Route::post('/returnmail', 'PageController@returnmail'); i still get page variable errors.

@JVMartin
Copy link
Owner

It's so much simpler than that. In your extended PageController...

public function post_returnmail()
{
    // Do your processing here, then...
    return $this->show('returnmail');  // <-- This will *automatically* load the page with an URL of 'returnmail'
}

Have your form post data here, to this method.

@JVMartin
Copy link
Owner

And if you need to return a custom view, then in a separate method:

public function returnmail()
{
    return View::make('pages.returnmail', $this->data);
}

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

You are rad man! I think I should be able to figure out the rest now:) I am a little unorthodox in my learning,.. Sorry about that..

As for passing an array as a 3rd parameter, is a really bad idea? :P

@JVMartin
Copy link
Owner

It's all good, I'm just glad to help. That show() method is one of the few methods in the framework where it's really critical to read it carefully and understand completely what it's doing... it loads the requested page from the $url variable and returns either the default page template view or a method by the same name if it finds a method. It will replace dashes with underscores when looking for that method.

All I know is that those functions only accept 2 parameters according to the documentation, so there's no reason to pass a 3rd.

I gotta run for a while, good luck and feel free to hit me up on Skype if you need any more help bro!

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

OMG IT WORKS. Thank you so much.

But there is now another issue, my text from the page edited from the WYSIWYG editor is showing on the top left!

http://i.gyazo.com/29ec2e806a9164536339eb1863616d14.png

@JVMartin
Copy link
Owner

Sweet! :) Can you paste me your pages.returnmail view? It's gotta be the view structure...

@gyoza
Copy link
Author

gyoza commented Feb 28, 2015

I figured it out :) I was missing the @Stop at the end. We are in business boys!

I'll make sure to highlight that we're using angel when my page goes live whenever it does for sure:)

Thanks again for being patient with the noob!

@JVMartin
Copy link
Owner

Most excellent! :)

No problemo amigo, and thanks - I really appreciate any links back to Angel (though they're not required) and I hope you have fun with it!

@gyoza gyoza changed the title Having some issues here extending the core. Having some issues here extending the core - total noob stuff here. Feb 28, 2015
@gyoza
Copy link
Author

gyoza commented Mar 1, 2015

So I found another issue, Following the tutorial you say to customize a page you just change the Controller to

                return View::make('pages.home', $this->data);

Now, yesterday you showed me how to load another page using using post, but I want to also customize the page as well and use modules from the admin page however, when I attempt to load the page which i created in the admin section and then its own page2.blade.php with

         public function post_returnpage2()
    {
            return $this->show('page2', $this->data);
    }

It ignores the page2.blade.php page completely. Am I doing something wrong?

@gyoza
Copy link
Author

gyoza commented Mar 1, 2015

My page2.blade.php for edification

@extends('core::template')

@section('title', $page->title)

@section('meta')
  {{ $page->meta_html() }}
  @stop

  @section('css')
    @if ($page->css)
        <style>
              {{ $page->css }}
                  </style>
                    @endif
                    @stop

                    @section('js')
                      @if ($page->js)
                          <script>
                                {{ $page->js }}
                                    </script>
                                      @endif
                                      @stop


                                      @section('content')

                                        {{ $page->html }}
                                        {{ $page->modules[0]->html}}
                                        test test
                                        test test
                                        @stop

@JVMartin
Copy link
Owner

JVMartin commented Mar 1, 2015

You're not using the show() method correctly.
https://github.com/JVMartin/angel/blob/master/src/controllers/PageController.php#L7

I explain the method in detail both earlier in this very thread and also in the YouTube tutorial. It's also only 8 lines of really easy-to-understand code, if you'd prefer to just read it yourself. It's never a good idea to use methods before reading them.

@gyoza
Copy link
Author

gyoza commented Mar 1, 2015

Shit my bad, i missed one of your updates :) Sorry to be a pain in the ass.

@gyoza gyoza changed the title Having some issues here extending the core - total noob stuff here. [solved] extending the core - total noob stuff here Mar 1, 2015
@gyoza
Copy link
Author

gyoza commented Mar 1, 2015

Thanks again man!

@JVMartin
Copy link
Owner

JVMartin commented Mar 1, 2015

It's all good man, no problem... so you said it's like a choose-your-own-adventure game? Let me know if you toss it up online, I'd like to check it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants