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

Help with Models and namespaces #7

Closed
ThinkDevStudios opened this issue Nov 20, 2015 · 9 comments
Closed

Help with Models and namespaces #7

ThinkDevStudios opened this issue Nov 20, 2015 · 9 comments

Comments

@ThinkDevStudios
Copy link

Hi! I am having issues running my models. I created a model called RegistrationModel and I want to call a function called init

when i call it via RegistrationModel::init() it says that RegistrationModel Class can not be found.

I edited the loading config file to
'MyTheme' => [themosis_path('theme').'controllers', themosis_path('theme').'models']

and added namespace in my class
namespace MyTheme\Models;
class RegistrationModel {

public function init() {

}

then try to use it in another file

use MyTheme\Models\RegistrationModel;
RegistrationModel::init();

I dont know what i am doing wrong. Hope you can guide me thanks!

@ThinkDevStudios
Copy link
Author

Got it working now.

@jlambe
Copy link
Contributor

jlambe commented Nov 20, 2015

@rovillesarate Can you please post your solution? because it seems I've made a mistake regarding PSR figure.

@jlambe
Copy link
Contributor

jlambe commented Nov 20, 2015

@rovillesarate Can you post your class namespace and the code from your loading.config.php file? Apparently, it's not psr-4 autoloading, but psr-0. It seems that I misunderstood the symfony api. Have you created extra folders to make your model class working?

@ThinkDevStudios
Copy link
Author

Yes. I also figure out that it is not using psr-4.
In the loading.config.php

return [
'MyTheme//' => [themosis_path('theme').'controllers', themosis_path('theme').'models']
];

Its not working when i try to use it as
namespace MyTheme/Models/ or MyTheme/Controller or just
namespace MyTheme since in the loading.config we already specified the directory to the controllers and model. It cant seem to be able to find the class no matter what you do. So if you try to call it
use MyTheme/Models/MyModel; MyModel::function(); It says it cant find the class name.

so I end up removing the key in the loading config. I also found out that the model and controller file name should be the same with its class name or it wont be recognized.

Plus i also forgot that my functions are not static functions so calling it by Class::function() wont work.

@jlambe
Copy link
Contributor

jlambe commented Nov 20, 2015

I've just released framework 1.2.1 with appropriate fix to use psr-4 autoloading. Make sure to run a composer update then copy/paste the bootstrap/start.php file located in the new themosis-theme (1.2.1) into your custom theme.

You should be able to use classes with or without namespaces correctly now.

Thanks for reporting this. Regards,

@ThinkDevStudios
Copy link
Author

do i need to still edit the loading config file to be able to use namespace?

@ThinkDevStudios
Copy link
Author

What is the namespace for Page::make() ?

use Themosis\Page; ? i am having problem with just the Page::make() If i remove it everything works fine.

@ThinkDevStudios
Copy link
Author

Got it working already. Its use Themosis\Facade\Page;

@jlambe
Copy link
Contributor

jlambe commented Nov 20, 2015

If you want to namespace your custom classes, yes, you need to update your loading.config.php by specifying your namespace as the key and your class paths as value.

All core classes work base on a facade as you found. Basically the Page API class fullname is Themosis\Facades\Page. But in order to simply use it as Page, the framework defines class aliases.

You can define custom aliases for your classes as well. For example, let's say you have a PageModel.php class inside your models with a namespace of MyTheme, the class fullname is MyTheme\PageModel. Now let's say you want to use that model under the Pm name, you'll define an alias in your application.config.php file like so:

[
    'MyTheme\\Models' => 'Pm'
]

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