Skip to content

Commit

Permalink
Merge 973eef1 into 7dbb550
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Jul 1, 2018
2 parents 7dbb550 + 973eef1 commit f625646
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
17 changes: 17 additions & 0 deletions masonite/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,23 @@ def _get_named_route(self, name, params):

return None

def _get_route_from_controller(self, controller):
web_routes = self.container.make('WebRoutes')

if not isinstance(controller, str):
module_location = controller.__module__
controller = controller.__qualname__.split('.')
else:
module_location = 'app.http.controllers'
controller = controller.split('@')

for route in web_routes:
if route.controller.__name__ == controller[0] and route.controller_method == controller[1] and route.module_location == module_location:
return route

def url_from_controller(self, controller, params = {}):
return self.compile_route_to_url(self._get_route_from_controller(controller).route_url, params)

def route(self, name, params = {}):
web_routes = self.container.make('WebRoutes')

Expand Down
18 changes: 18 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from config import application, providers
from pydoc import locate
from app.http.test_controllers.TestController import TestController

from masonite.request import Request
from masonite.app import App
Expand Down Expand Up @@ -340,6 +341,22 @@ def test_request_sets_request_method(self):
assert request.input('__method') == 'PUT'
assert request.get_request_method() == 'PUT'


def test_request_url_from_controller(self):
app = App()
app.bind('Request', self.request)
app.bind('WebRoutes', [
get('/test/url', 'TestController@show').name('test.url'),
get('/test/url/@id', 'ControllerTest@show').name('test.id'),
get('/test/url/controller/@id', TestController.show).name('test.controller'),
])

request = app.make('Request').load_app(app)

assert request.url_from_controller('TestController@show') == '/test/url'
assert request.url_from_controller('ControllerTest@show', {'id': 1}) == '/test/url/1'
assert request.url_from_controller(TestController.show, {'id': 1}) == '/test/url/controller/1'

def test_contains_for_path_detection(self):
self.request.path = '/test/path'
assert self.request.contains('/test/*')
Expand All @@ -363,3 +380,4 @@ def test_contains_multiple_asteriks(self):
self.request.path = '/dashboard/user/edit/1'
assert self.request.contains('/dashboard/user/*:string/*:int')


0 comments on commit f625646

Please sign in to comment.